public class ConstTest { //只能在定义时声明 public const int ConstInt = 1000; public readonly int ReadOnlyInt = 100; public static int StaticInt = 150; public ConstTest() { ReadOnlyInt = 101; StaticInt = 151; } //static 前面不可加修饰符 static ConstTest() { //此处只能初始化static变量 StaticInt = 152; } }
class Program { public static void Main(string[] args) { Console.WriteLine(ConstTest.ConstInt);//输出1000 Console.WriteLine(ConstTest.StaticInt);//输出152 ConstTest mc = new ConstTest(); Console.WriteLine(ConstTest.StaticInt);//输出151 } }