description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid | ||
---|---|---|---|---|---|---|---|
Compiler Error CS0134 |
Compiler Error CS0134 |
07/20/2015 |
|
|
c7b57de2-42ad-473e-8e45-8ac7a0caea9a |
'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";
}