Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Compiler Error CS0134
Collapse the table of content
Expand the table of content

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.

The following example generates CS0134.

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

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

   const MyTest test2 = null;
   const System.String test3 = "test";
© 2015 Microsoft