Passing objects by reference or not in C#
Solution 1
Since SomeClass is a class, then it is automatically passed by reference to the AddToList method (or more accurately, its reference is passed by value) so the object is not copied. You only need to use the ref keyword if you want to re-assign the object the reference points to in the AddToList method e.g. Item = new SomeClass();
.
Solution 2
Since SomeClass is a reference type, you do not need to use the "ref" keyword. If it were a value type, "ref" might be useful.
Solution 3
Think of out
as a way of making a parameter work as a return value.
So these are very similar:
void Foo(out int result)
{
result = 5;
}
int Foo()
{
return 5;
}
And then think of ref
as a way of allowing a parameter to be both an input and an output.
So in your example, if you declared your method:
public void AddToList(ref SomeClass Item)
Then the caller would have to write something like:
SomeClass i = null;
obj.AddToList(ref i);
This would be illegal, for example:
obj.AddToList(ref new SomeClass());
They would be forced to pass a variable name, rather than an expression, so that the AddToList
method can store a value in the variable. By adding the ref
prefix you are allowing your method to make the passed variable point to a different object.
Diego Bascans
I used to be a full-time software developer, but now teach IT in a school and do the coding in my spare time.
Updated on June 04, 2022Comments
-
Diego Bascans almost 2 years
Suppose I have a class like this:
public class ThingManager { List<SomeClass> ItemList; public void AddToList (SomeClass Item) { ItemList.Add(Item); } public void ProcessListItems() { // go through list one item at a time, get item from list, // modify item according to class' purpose } }
Assume "SomeClass" is a fairly large class containing methods and members that are quite complex (List<>s and arrays, for example) and that there may be a large quantity of them, so not copying vast amounts of data around the program is important.
Should the "AddToList" method have "ref" in it or not? And why?
It's like trying to learn pointers in C all over again ;-) (which is probably why I am getting confused, I'm trying to relate these to pointers. In C it'd be "SomeClass *Item" and a list of "SomeClass *" variables)
-
Zayn_SE about 14 yearsThis is an excellent overview of parameter passing in C#.
-
Matti Virkkunen about 14 yearsWhat is it with people randomly adding the best-practices tag to their questions... isn't it obvious that if you ask something, you're looking for the best possible answer? At least this one doesn't have the design-patterns tag...
-
Diego Bascans about 14 yearsThank you for your very helpful comments, they really added to this question.
-
-
Daniel Earwicker about 14 yearsThat's not necessarily true. The
ref
(orout
) keyword is useful with reference type variables also. -
TreDubZedd about 14 yearsIn Piku's case, it didn't sound like he was attempting to change the object, merely reference it. I agree, though, that had he needed to change the object, "ref" would have been the way to go ("out" would have required a different scenario entirely).
-
Josh O'Bryan almost 10 yearsAll too often I see people use ref here at work when they don't need to. Don't use it if you don't know what it does. You'll know when you need it, just like Lee mentions.