在此示例中,将创建一个静态Demo类,并声明一个静态变量计数。
这里count变量被视为全局变量。因此在示例中,它一直在增加,因为仅创建了该类的一个实例
static class Demo{
public static int count;
static Demo(){
System.Console.WriteLine("Static Constuctor called");
}
}
class Program{
static void Main(){
Demo.count++;
Demo.count++;
System.Console.WriteLine(Demo.count);
Console.ReadKey();
}
}输出结果
Static Constuctor called 2