This documentation is archived and is not being maintained.

Compiler Error CS1604

Cannot assign to 'variable' because it is read-only

An assignment was made to a read-only variable.

The following sample generates CS1604:

// CS1604.cs
class NoWrite
{
   public static void Main()
   {
      int [] a = {1,2,3};

      foreach(int k in a)
      {
         k = k + 1;   // CS1604, k is read-only
         System.Console.WriteLine(k);
      }
   }
}
Show: