Export (0) Print
Expand All
Expand Minimize

Compiler Error CS1654 

Error Message

Cannot modify members of 'variable' because it is a 'read-only variable type'

This error occurs when you attempt to modify members of a variable which is read-only because it is in a special construct.

Example

The following example generates error CS1654.

// CS1654.cs
using System;
using System.Collections;

public struct Test : IEnumerable
{
    private int index;
    public int Index
    {
        get { return index; }
        set { index = value; }
    }

    public IEnumerator GetEnumerator()
    {
        for(int i = 0; i< 10; i++)
            yield return this;
        yield break;
    }
}

public class Repro
{
    static int Main()
    {
        int i = 0;
        Test t = new Test();
        foreach (Test tt in t)
        {
            tt.Index = i++;    // CS1654
        }
        return 1;
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft