所以这有效..
public MyClass(ref Apple apple)
{
apple = new Apple("Macintosh"); // Works fine
}
Run Code Online (Sandbox Code Playgroud)
但有可能做这样的事情吗?
private Apple myApple;
public MyClass(ref Apple apple)
{
myApple = apple;
}
public void ModifyApple()
{
myApple = new Apple("Macintosh"); // does not change the input variable like the first example did
}
Run Code Online (Sandbox Code Playgroud)
当ref变量被复制到成员变量时,myApple它似乎失去了它的'ref-ness'并且重新赋值它不再改变输入变量.有没有解决的办法?