Passing reference type with 'ref' vs. without

G

Guest

Hi,

Could someone clarify my confusion regarding passing reference types to a
method with ref keyword and explain when it's practical to use it.

It's my understanding that in .NET reference types hold a reference to an
object as opposed to object data itself. So, when reference type parameter is
passed into a method, a copy of objects reference is passed in, so called
method can do whatever to "original" object and a caller will see those
changes.
But if one adds "ref" keyword the actual address of a variable that holds
object's reference is passed in, Which accomplishes the same thing as above
scenario (without ref), but far more dangerous. My questions:

1. Is my overall understanding accurate?
2. What are some practical (real life) usages for passing reference type
with ref.
3. Is there any performance benefits ref vs. without ref.


Thanks
 
O

Oliver Sturm

Lenn said:
It's my understanding that in .NET reference types hold a reference to an
object as opposed to object data itself. So, when reference type parameter is
passed into a method, a copy of objects reference is passed in, so called
method can do whatever to "original" object and a caller will see those
changes.
But if one adds "ref" keyword the actual address of a variable that holds
object's reference is passed in, Which accomplishes the same thing as above
scenario (without ref), but far more dangerous. My questions:

1. Is my overall understanding accurate?

I'm not sure, but maybe not quite. The thing is this: when you use the
ref keyword, the parameter passed is a reference to whatever there was
before, regardless of whether that was already a reference type or not.
2. What are some practical (real life) usages for passing reference type
with ref.

Think of it like this: most of the time you use functions that return
values:

int Foo() {
return 42;
}

...
int bar = Foo(); // bar is now 42

You could achieve the exact same result by going:

void Foo(ref int parm) {
parm = 42;
}

...
int bar;
Foo(ref bar); // bar is now 42

The main difference is that it's possible to have more than one
parameter at a time that is set in this way, which is not possible with
"normal" return values (in C# that is, it's perfectly possible in other
languages). C# strives to make this visible to the programmer who calls
such a method by making it mandatory to use the ref keyword on the
method signature as well as for the method call.

I think if ref parameters are used in a responsible manner, they can be
a useful tool in specific circumstances. But these circumstances are
really quite specific indeed - always consider these questions:

a) Are you sure that the method in question must calculate several
values for you at once? Is this the best implementation?

b) If you said yes to (a), wouldn't it be a better idea to find some
encapsulation for the data in question, like a class or struct that
holds the various values?

Personally, I haven't used ref parameters for quite a while, because I
rarely find the specific situation where they seem to be the best solution.

3. Is there any performance benefits ref vs. without ref.

You can't really ask the question like that - either you need them or
you don't. Generally speaking, I don't think there's a benefit or
penalty either way. It's just one more pointer, after all, whatever they
call it :)



Oliver Sturm
 
S

sdbillsfan

The difference between the two is that you can alter what a ref
parameter points to. Consider the following example:

public void RefVar(ref MyClass x, MyClass y)
{
x = new MyClass(); //x will now point to this new class instance
inside and ouside this method scope
y = new MyClass(); //will only point to this new class instance
inside this method scope
}

Usually ref variables are used in procedures that do initialization of
objects (means the method should accept a null reference). A "factory"
type procedure could do this (although usually you'd just use the
return value).

Performance is not a factor when deciding whether to pass ref
arguments, it just depends on what your function needs to do.
 
G

Guest

The point of the ref keyword is to allow you to change the object that is
being passed into the method inside the method.

For example:

void DoSomething(ArrayList exampleList)
{
exampleList.Add("Something");
}

The caller of this method will see the changes made to the arraylist....
i.e. it will see the new entry.

However, with

void DoSomething(ArrayList exampleList)
{
exampleList = new ArrayList();
exampleList.Add("Something");
}

the caller will not see the new item as the reference inside the method has
changed. For the caller to be able to see the new item in the new list the
method signature would be:

void DoSomething(ref ArrayList exampleList)

And, ref is used for allowing you to pass value types by reference.
 
W

Wessel Troost

Could someone clarify my confusion regarding passing reference types to a
method with ref keyword and explain when it's practical to use it.
The reference itself can be updated if passed with "ref".

void Function1(ref x)
{
// Affects the function calling it; it's X now points to Y.
x = y;
}

void ByVal(x)
{
// Affects only our local x.
x = y;
}


For a better explanation ee this article by Jon Skeet:
http://www.yoda.arachsys.com/csharp/parameters.html
scenario (without ref), but far more dangerous. My questions:
Dangerous? Why dangerous?
1. Is my overall understanding accurate?

Yeah I think so.
2. What are some practical (real life) usages for passing reference type
with ref.

If you wish to return two new references. There's only one return value,
so you'd have to use a ref or out parameter to pass the reference back to
the caller.
3. Is there any performance benefits ref vs. without ref.
Without ref would use one lesser dereference. But I don't think that
matters at all, in normal scenarios.

Greetings,
Wessel
 
M

Mike

Others have explained well with examples.

One way to think about it, is that C# (as well as Java, Lisp, Python,
etc...) by default (or without exception) pass
*object references by value*.
By specifiying "ref" you can pass a reference (address) of something that
holds an object reference. When passing a C# struct you pass an object by
value.

m
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top