我有以下代码:
static void Main(string[] args)
{
myclass c = new myclass();
c.test1 = 1;
myclass c2 = TestPassByValByRef(c);
Console.WriteLine("c.Test1: {0}", c.test1);
Console.WriteLine("c2.Test1: {0}", c2.test1);
Console.ReadLine();
}
private static myclass TestPassByValByRef(myclass c)
{
Console.WriteLine("Before NowPassByRef c.Test1: {0}", c.test1);
NowPassByRef(ref c);
Console.WriteLine("After NowPassByRef c.Test1: {0}", c.test1);
return c;
}
private static void NowPassByRef(ref myclass c)
{
c = new myclass();
c.test1 = 10;
c.test2 = 25;
}
输出是 c2 保留更改的值,而 c 没有。我的问题是:c
in会发生什么TestPassByValByRef
?