更新:2007 年 11 月
错误消息
无法对静态只读字段“name”的字段赋值(静态构造函数或变量初始值设定项中除外)
readonly 变量与要在其中初始化它的构造函数必须具有相同的 static 用法。有关更多信息,请参见静态构造函数(C# 编程指南)。
下面的示例生成 CS0198:
// CS0198.cs
class MyClass
{
public static readonly int TestInt = 6;
MyClass()
{
TestInt = 11; // CS0198, constructor is not static and readonly field is
}
public static void Main()
{
}
}