コンパイラ エラー CS0134
更新 : 2007 年 11 月
エラー メッセージ
'変数' の型は '型' です。文字列以外の参照型の const フィールドは null でのみ初期化できます。
constant-expression は、コンパイル時に完全に評価できる式です。参照型の非 null 値を作成するには new 演算子を適用するしかなく、new 演算子は constant-expression で許可されていないので、string 以外の "参照型" の定数が取り得る値は null です。
const 文字配列を作成しようとしてこのエラーが発生した場合、配列を readonly にし、コンストラクタ内で初期化します。
使用例
次の例では 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";
}