public class TestClass
{
public int testInt;
};
public void TestFunc(Action<TestClass> action)
{
var testClass = new TestClass();
action(testClass);
Console.WriteLine(testClass.testInt); // still 0 ?!
}
public void SomeOtherFunc()
{
var newTestClass = new TestClass()
{
testInt = 1
};
TestFunc(testClass =>
{
testClass = newTestClass;
});
}
鉴于上面的代码,我希望执行动作后 TestFunc 中的 TestClass 实例具有 testInt = 1,但是事实并非如此,它仍然为零。实际上,看起来任务没有发生。这对我来说很奇怪,因为默认情况下类应该通过引用传递,并且我认为通过该操作也可以通过引用传递类。显然情况并非如此,我现在缺少我应该真正知道的东西。我对此一无所获,我需要有人向我解释为什么会这样。谢谢。