Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
'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
//OK
const MyTest test2 = null;
const System.String test3 = "test";
}
.NET feedback
.NET is an open source project. Select a link to provide feedback: