What is the difference between
public function Foo(ref Bar bar) { bar.Prop = 1; } public function Foo(Bar bar) { bar.Prop = 1; }
essentially what is the point of ‘ref’. isn’t an object always by reference?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
nullor to a different reference. Withrefthis change affects the caller’s variable; withoutrefit 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.