Difference between 'out' and 'ref' parameters.

C

Chris Mayers

'out' and 'ref' parameters in C#...

Both these can be used to pass parameter values BACK from a Method, but
obviously they are different techniques.

As I understand it,

'ref' parameter passes the object into the Method by Reference, ie passes
the memory location of the original object in the parameter list. The Method
may change the value of the object and by doing so, will be changing the
value of the original object.

'out' parameter passes an object BY VALUE back into the object in the
calling parameters.

1) Am I right?
2) Is it true to say that you are usually better to use 'out' unless the
original value of the parameter being passed in effects its value when you
pass it back. Or if your method needs to return more than one calculated
result.

eg

private void GetPosition(string lorryId, out int xLocation, out int
yLocation)

Hope this makes sense, I'm very tired...

Thanks,

Chris.
 
L

LOZANO-MORÁN, Gabriel

Both by reference, difference is that if you use an out parameter you don't
need to initialize that parameter variable. So if you want to do the
initialization in a method you can use a out parameter.

Ref (initialize the variable)
int getal = 0;
RefTest(ref getal);

Out (no need to intialize the variable)
int getal;
OutTest(out getal);

Gabriel Lozano-Morán
 
G

Guest

You're almost right, "ref" and "out" are similar in that they can both return
a value from a method, but they have slightly different semantics.

You need to initialize a "ref" parameter before passing it to a method, but
you don't have to initialize an "out" parameter before passing it to a method
because the c# compiler knows that the method will assign a value to the
parameter before returning. In fact, the c# compiler will throw a compiler
error if it detects that the method has not assigned a value to an "out"
parameter before returning.

By value example: The output from the code below will be "x = 0"

int x = 0;
add(x)
Console.Write("x = {0}", x);

void add(ref x)
{
x = x + 1
}

"Ref" example: The output from the code below will be "x = 1"

int x = 0;
add(ref x)
Console.Write("x = {0}", x);

void add(ref x)
{
x = x + 1
}

"out" example: The output from the code below will be "x = 1"

int x;
add(out x)
Console.Write("x = {0}", x);

void add(out x)
{
x = x + 1
}

Note: The variable "x" is not initialized prior to the call to "add"

For more info:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfref.asp
 
C

Chris Mayers

Thanks for your answer, that has helped a lot.

There does seem to be another difference though,

this is legal (Compiler allows it)

private int DoStuff(ref string x)
{
x = x + ":";
return -1;
}

wheras this will not compile:

private int DoStuff(out string x)
{
x = x + ":";
return -1;
}

(says " 'Use of unassigned local vairable 'x' ")

This, however is fine:

private into DoStuff(out string x)
{
x = "1";
return -1;
}

So you can't pass a value INTO a method using a 'out' parameter like you can
with a 'ref' parameter. (?) I guess that's why its called 'out' :)

Chris
 
G

Guest

I'm glad I helped you out, and it sounds like you've got a good understanding
of the differences between "ref" and "out" now.
 
S

Sean Hederman

[Snip]
So you can't pass a value INTO a method using a 'out' parameter like you
can
with a 'ref' parameter. (?) I guess that's why its called 'out' :)

Exactly. An out parameter for your function acts like an uninitialized
variable. You have to initialize it before using it. The corollary is that
the caller does not have to initialize the value they pass in. With ref, the
caller must initialize the variable, and the callee doesn't have to.
 
C

Chris Mayers

Jorge,

Did you post a reply to this?
I saw somthing from you, but the news-server seemed to delete it before my
news reader got a chance to down load it...??
 
G

Guest

Just a comment that I think you have a good understanding of the difference
between "ref" and "out" now.
 
C

Chris Mayers

Thanks everyone for your help.

Jorge:Your message dissapeared again from my newsreader (Outlook Express),
but I managed to find in on Google Groups...

I shall go and find somthing else to try and understand now...

Cheers,

Chris.
 

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