先看一个程序:

一点疑问:关于static 和static readonlyusing System;
一点疑问:关于static 和static readonly
一点疑问:关于static 和static readonly
class Test

一个字段被声明为static readonly,则不可以在构造函数里被赋值

我的理解是一个字段被声明为静态的(static),则它只能实类名而不是对象名来访问
构造函数是在实例化一个类的时候被调用,而对于静态的字段,放在构造函数里是没有意义的.
解决办法是把它放在静态构造函数中.

一点疑问:关于static 和static readonlystatic Test()
}

再看一个程序:

一点疑问:关于static 和static readonlyusing System;
一点疑问:关于static 和static readonly
一点疑问:关于static 和static readonly
class Test

此程序编译通过...
输出结果为 300,0

若改成

一点疑问:关于static 和static readonlypublic static void Main()
    }

则输出结果为 300,400

d被声明为静态,却不必放在静态构造函数中..?

相关文章: