I know this is an old question, but none of the answers give a good direct reason why in my opinion.
You don't need to use ref in this instance, and here's why. Consider this function:
void Foo(MyClass a1, ref MyClass a2, out MyClass b1, int c1, MyStruct d1, ref MyStruct d2)
{
}
Now call this function as
MyClass a = new MyClass();
MyClass b = null
int c = 3;
MyStruct d = new MyStruct();
Foo(a, ref a, b, c, d, ref d);
Here's what you get inside of the function:
void Foo(MyClass a1, ref MyClass a2,
out MyClass b1,
int c1,
MyStruct d1, ref MyStruct d2)
{
a1 is a copy in memory of the pointer to the instantiated class a;
a2 is the pointer to the instantiated class a;
b1 is the pointer to b, but has the additional check of having to be set within this function - and cannot be used before being set;
c1 is a copy in memory of the variable c;
d1 is a copy in memory of the struct d;
d2 is the struct d;
}
Important things to realize:
- setting
a1 to null will not set a to null.
- setting
a2 to null will set a to null.
- setting
b1 is required.
- setting
c1 will not change c.
- setting
d1 will not change d.
- setting
d2 will change d.
This allows for some weirdness like this:
void Foo(MyClass x, ref MyClass y)
{
x = null;
y.Bar("hi");
}
Called like:
MyClass a = new MyClass();
Foo(a, ref a);
You are using a class, and so your situation is more like variable a1 in the function call. Which means ref isn't strictly required.
The Jon Skeet article won't help you that much because his example with IntHolder is a struct not a class. Struct is value type like int and must be handled the same way.