What is the use of "ref" for reference-type variables in C#?
Solution 1
You can change what foo
points to using y
:
Foo foo = new Foo("1");
void Bar(ref Foo y)
{
y = new Foo("2");
}
Bar(ref foo);
// foo.Name == "2"
Solution 2
There are cases where you want to modify the actual reference and not the object pointed to:
void Swap<T>(ref T x, ref T y) {
T t = x;
x = y;
y = t;
}
var test = new[] { "0", "1" };
Swap(ref test[0], ref test[1]);
Solution 3
Jon Skeet wrote a great article about parameter passing in C#. It details clearly the exact behaviour and usage of passing parameters by value, by reference (ref
), and by output (out
).
Here's an important quote from that page in relation to ref
parameters:
Reference parameters don't pass the values of the variables used in the function member invocation - they use the variables themselves. Rather than creating a new storage location for the variable in the function member declaration, the same storage location is used, so the value of the variable in the function member and the value of the reference parameter will always be the same. Reference parameters need the ref modifier as part of both the declaration and the invocation - that means it's always clear when you're passing something by reference.
Solution 4
Very nicely explained here : http://msdn.microsoft.com/en-us/library/s6938f28.aspx
Abstract from the article:
A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block. To do that, pass the parameter using the ref or out keyword.
Solution 5
When you pass a reference type with the ref keyword, you pass the reference by reference, and the method you call can assign a new value to the parameter. That change will propagate to the calling scope. Without ref, the reference is passed by value, and this doesn't happen.
C# also has the 'out' keyword which is a lot like ref, except that with 'ref', arguments must be initialized before calling the method, and with 'out' you must assign a value in the receiving method.
Related videos on Youtube
Andreas Grech
+++++[>+++++[>++++<-]<-]>>+++.--..++++++. Contactable at $(echo qernfterpu#tznvy?pbz | tr ?a-z# .n-za-m@)
Updated on March 18, 2020Comments
-
Andreas Grech about 4 years
I understand that if I pass a value-type (
int
,struct
, etc.) as a parameter (without theref
keyword), a copy of that variable is passed to the method, but if I use theref
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 theref
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"; }
-
lhahne almost 15 yearsso you basically get a reference to the original reference
-
Sam almost 15 yearsYou can change what the original reference 'refers' to, so yes.
-
corlettk almost 15 yearsI like the analogy of passing your dogs leash to friend for passing a reference by-value... it breaks down quickly though, because I think you would probably notice if your friend traded-up your shitzu to a doberman before he handed you back the leash ;-)
-
Andreas Grech almost 15 yearsChris, your explanation is great; Thanks for helping me understand this concept.
-
Tom Hazel about 12 yearsSo using 'ref' on an object is like using double pointers in C++?
-
Sam about 12 years@TomHazel: -ish, provided you're using "double" pointers in C++ to change what a pointer points to.
-
Marcel over 9 yearsThe explanation is indeed very nice. However, link-only answers are discouraged on SO. I added a summary from the article, as a convenience to the readers here.
-
Alexander Derck almost 8 yearsAlso you make clear to the readers of your function that the value of the parameter might change, which wouldn't be expected without the
ref
keyword. -
Chuck Lu over 3 yearsThis answer gives a situation why I need to change the original reference rather than set the property value of previous instance.
-
Chuck Lu over 3 yearsThis answer did not give the reason why I need to change the original reference instead of set the property value of original reference.