Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

Compiler Error CS0191

A readonly field cannot be assigned to (except in a constructor or a variable initializer)

A readonly field can only take an assignment in a constructor or at declaration.

The following sample generates CS0191:

// CS0191.cs
class MyClass
   public readonly int TestInt = 6;  // OK to assign to readonly field in declaration

      TestInt = 11;                  // OK to assign to readonly field in constructor

   public void TestReadOnly()
      TestInt = 19;                  // CS0191

   public static void Main()

CS0191 also results if the readonly field is static and the constructor is not marked static.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.