[C#] C#同一类的各实例间共享变量

各实例间共享类的静态变量

public class Test
{
    private static int a;

    public int GetA()
    {
        return a;
    }

    public void SetA(int i)
    {
        a = i;
    }
}

var t1 = new Test();
var t2 = new Test();

t1.SetA(1);
Console.WriteLine(t2.GetA());   //1

t2.SetA(2);
Console.WriteLine(t1.GetA());   //2

你可能感兴趣的:([C#] C#同一类的各实例间共享变量)