473,324 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Difference between 'out' and 'ref' parameters.

'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.
Nov 22 '05 #1
8 5381
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

"Chris Mayers" <ch**************@SUEDEYahoo.Com> wrote in message
news:#4**************@TK2MSFTNGP14.phx.gbl...
'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.

Nov 22 '05 #2
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/de...l/vclrfref.asp

"Chris Mayers" wrote:
'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.

Nov 22 '05 #3
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
Nov 22 '05 #4
"Chris Mayers" <ch**************@SUEDEYahoo.Com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
[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.
Chris

Nov 22 '05 #5
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.

"Chris Mayers" wrote:
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

Nov 22 '05 #6
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...??
"Chris Mayers" <ch**************@SUEDEYahoo.Com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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

Nov 22 '05 #7
Just a comment that I think you have a good understanding of the difference
between "ref" and "out" now.

"Chris Mayers" wrote:
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...??
"Chris Mayers" <ch**************@SUEDEYahoo.Com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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


Nov 22 '05 #8
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.

"Chris Mayers" <ch**************@SUEDEYahoo.Com> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
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...??
"Chris Mayers" <ch**************@SUEDEYahoo.Com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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


Nov 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Chris Mayers | last post by:
'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...
2
by: Mountain Bikn' Guy | last post by:
It is known that one cannot pass arguments as ref or out in a marshal-by-reference class. My problem is that I have a C DLL (and C# wrapper) that I need to isolate in an AppDomain and then I need...
3
by: C# Learner | last post by:
Note ---- Please use a fixed-width font to view this, such as Courier New. Problem
10
by: Jon Davis | last post by:
I've been using the ref keyword as a way of passing strings to methods as references so that in the method I don't have to manually return the modified "version" of what was passed into the method,...
3
by: SamoK | last post by:
Hy! I'd like to know how can I use ref and out parameters in c++. I'd like to send (from C#) to my C++.net function a variable by reference, so I could change it in my c++.net function. And...
4
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I have been doing a lot of PInvoke calls using signatures published on www.pinvoke.net. I notice that where out parameters are involved, sometimes the signature uses 'out' and sometimes ''. I have...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.