Share via


Compiler Error CS0134

'variable' is of type 'type'. A const field of a reference type other than string can only be initialized with null.

A constant-expression is an expression that can be fully evaluated at compile-time. Because the only way to create a non-null value of a reference-type is to apply the new operator, and because the new operator is not permitted in a constant-expression, the only possible value for constants of reference-types other than string is null.

If you encounter this error by trying to create a const string array, the solution is to make the array readonly, and initialize it in the constructor.

Example

The following example generates CS0134.

// CS0134.cs
// compile with: /target:library
class MyTest {} 

class MyClass
{
   const MyTest test = new MyTest();   // CS0134

   //OK
   const MyTest test2 = null;
   const System.String test3 = "test";
}