c#struct 和class 的区别
天上飞猪 2018-09-05 03:13:01 看如下代码
class Program
{
private class Test {
private string a;
public string A
{
get { return a; }
set { a = value; }
}
}
static void Main(string[] args)
{
Test t1 = new Test();
t1.A = "1";
Test t2 = t1;
Console.WriteLine("T1的值是{0}", t1.A);
Console.WriteLine("T2的值是{0}", t2.A);
t2.A ="2";
Console.WriteLine("T1的值是{0}", t1.A);
Console.WriteLine("T2的值是{0}", t2.A);
}
}
以上代码输出为1 1 2 2
如果把class改为struct 输出为1 ,1,1,2
为什么class的时候 输出为 1 1 22 是因为同一个引用吗