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 } }