Reputation: 195
Quick question : I am passing a class (reference type) to a method without using the "ref" keyword. Thus, the reference itself to my class is passed by value. Then, I change the reference of my class (I make the reference point to another instance defined inside my method).
Finally, I return the initial method. However, in this case the returned instances points to the instance of the second class.
public Class Foo(Class A)
{
Class B = new Class();
A = B;
return A;
}
Foo returns a references pointing to B ! I am a little bit confused, since when doing A = B I make the reference of A point to another reference, or A's referenced is passed by value.
EDIT 1 Thanks for the response, but If I take the following example the change is not reflected. Indeed, I am trying to change the references of A but A's references is passed by value so in this case I understand why the change is not reflected..
void Foo(Class A)
{
A = null;
}
Many Thanks.
Upvotes: 1
Views: 105
Reputation: 59705
You are passing a reference - an immutable value - into the method using the mutable variable, more precisely a parameter, A
. Then you assign to the mutable variable A
a new value, the immutable reference to the newly created object. Finally you are returning the current value of the variable A
which at that point is the reference to the new object and no longer the reference to the object you passed into the method.
Essentially you are confusing the variable and the value stored in that variable. At no point did you change any reference, you only exchanged the value, i.e. reference, stored in the variable.
Upvotes: 1
Reputation: 11783
Basically, when you're passing an object, a reference is passed:
When an object of a reference type is passed to a method, a reference to the object is passed. That is, the method receives not the object itself but an argument that indicates the location of the object. If you change a member of the object by using this reference, the change is reflected in the argument in the calling method, even if you pass the object by value.
You can read more on this MSDN page.
You can read more on the other answers, but do notice that you're returning a class. Usually you'll return a specific object type, and when you won't be so free to do silly things like that. (of course, assuming that B
inherits from A
, you could create a new B
inside the method and return it, which will be valid, but still, it's not making sense).
Another thing you might want to remember is the ref
and out
.
ref
will expect an initialized value, and that value is changed in the method.
out
doesn't care what it gets in, but you need to initialize and set it in the method.
Other than that, and the other answers here, either be more specific with your question and code, or have a read at the different links in the answers :)
Upvotes: 3
Reputation: 3173
When you pass reference type by value the attempt to reassign the parameter to a different memory location only works inside the method and does not affect the original variable.
Check out Passing reference - type parameters .
Upvotes: 1