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

Compiler Error CS0198

Fields of static readonly field 'name' cannot be assigned to (except in a static constructor or a variable initializer)

A readonly variable must have the same static usage as the constructor in which you want to initialize it. For more information, see Static Constructors (C# Programming Guide).

The following sample generates CS0198:

// CS0198.cs
class MyClass
{
   public static readonly int TestInt = 6;

   MyClass()
   {
      TestInt = 11;   // CS0198, constructor is not static and readonly field is
   }

   public static void Main()
   {
   }
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.