简单点就是静态的全局变量,在过程被执行或者调用时被赋值,后面接着被调用时不会在赋初值。
static void Main(string[] args)
{
TestStatic ta = new TestStatic();
Console.WriteLine("1st time I is {0}\n", ta.Addi());
TestStatic tb = new TestStatic();
Console.WriteLine("2nd time I is {0}\n", tb.Addi());
TestStatic tc = new TestStatic();
Console.WriteLine("3rd time I is {0}\n", tc.Addi());
Console.WriteLine("i={0}", TestStatic.i);
Console.ReadLine();
}
class TestStatic
{
public static int i = 10;
public int Addi()
{
i = i + 1;
return i;
}
}
如上Testsataic类中i,在ta实例化时i已经被赋值为11了,后面tb实例化时已经是12了,和auto 的区别是,auto每次被实例化时都会赋初值。
在类中如果被定义为了static,则可以不用实例化就能通过类名直接访问。