passing in object by ref

我是研究僧i 提交于 2019-12-01 22:01:45

The point is that you never actually pass an object. You pass a reference - and the argument itself can be passed by reference or value. They behave differently if you change the parameter value itself, e.g. setting it to null or to a different reference. With ref this change affects the caller's variable; without ref it was only a copy of the value which was passed, so the caller doesn't see any change to their variable.

See my article on argument passing for more details.

Yes. But if you were to do this:

public function Foo(ref Bar bar)
{
   bar = new Bar();
}

public function Foo(Bar bar)
{
    bar = new Bar();
}

then you'd see the difference. The first passes a reference to the reference, and so in this case bar gets changed to your new object. In the second, it doesn't.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!