I understand that if I pass a value-type (int
, struct
, etc.) as a parameter (without the ref
keyword), a copy of that variable is passed to the method, but if I use the ref
keyword a reference to that variable is passed, not a new one.
But with reference-types, like classes, even without the ref
keyword, a reference is passed to the method, not a copy. So what is the use of the ref
keyword with reference-types?
Take for example:
var x = new Foo();
What is the difference between the following?
void Bar(Foo y) {
y.Name = "2";
}
and
void Bar(ref Foo y) {
y.Name = "2";
}
In addition to the existing answers:
As you asked for the difference of the 2 methods: There is no co(ntra)variance when using
ref
orout
:Another bunch of code
Very nicely explained here : http://msdn.microsoft.com/en-us/library/s6938f28.aspx
Abstract from the article:
It allows you to modify the reference passed in. e.g.
You can also use out if you don't care about the reference passed in:
Reference Variables carry the address from one place to another so any updation on them at any place will reflect on all the places THEN what is the use of REF. Reference variable are good till no new memory is allocated to the reference variable passed in the method. Once new memory allocate then the value change on this will not reflect everywhere. For this ref comes. Ref is reference of reference so whenever new memory allocate it get to know because it is pointing to that location therefore the value can be shared by everyOne. You can see the image for more clearity.
A parameter in a method seems to be always passing a copy, the question is a copy of what. A copy is done by a copy constructor for an object and since all variables are Object in C#, i believe this is the case for all of them. Variables(objects) are like people living at some addresses. We either change the people living at those addresses or we can create more references to the people living at those addresses in the phone book(make shallow copies). So, more than one identifier can refer to the same address. Reference types desire more space, so unlike value types that are directly connected by an arrow to their identifier in the stack, they have value for another address in the heap( a bigger space to dwell). This space needs to be taken from the heap.
Value type: Indentifier(contains value =address of stack value)---->Value of value type
Reference type: Identifier(contains value=address of stack value)---->(contains value=address of heap value)---->Heap value(most often contains addresses to other values), imagine more arrows sticking in different directions to Array[0], Array[1], array[2]
The only way to change a value is to follow the arrows. If one arrow gets lost/changed in the way the value is unreachable.