大家好,今天闲暇之际发现了一个东西,列出下面一段程序,大家请看:
static void Main(string[] args)
{
C c = new C(1);
S s = c.s;
s.X = 2;
Console.WriteLine("c-s-X:" + c.s.X);
Console.ReadKey();
}
struct S
{
public int X { get; set; }
public int Y { get; set; }
}
class C
{
public S s { get; set; }
public int Z { get; set; }
public C(int z)
{
this.Z = z;
}
}
大家这个这个会输出啥吗?
很多人应该不难看出这个是值类型传递的问题,传递的是副本,所以输出的c.s.X仍然为0