通过文献传递对象 [英] passing in object by ref
本文介绍了通过文献传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public function Foo(ref Bar bar)
{
bar.Prop = 1;
}
public function Foo(Bar bar)
{
bar.Prop = 1;
}
基本上什么是REF的点。是不是对象总是通过引用?
essentially what is the point of "ref". isn't an object always by reference?
推荐答案
的一点是,你永远不会真正传递的对象的。您传递的引用的 - 和参数本身可以通过引用或值传递。他们不同的表现,如果你改变参数值本身,例如:将其设置为空
或不同的参考。随着 REF
这种变化会影响调用者的变量;没有 REF
这只是一个的复制的被传递的价值,所以调用者没有看到任何改变的变量。
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.
请参阅我的论点的文章传递更多细节。
这篇关于通过文献传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文