'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. 8 5391
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.
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.
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
"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
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
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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
by: C# Learner |
last post by:
Note
----
Please use a fixed-width font to view this, such as Courier New.
Problem
|
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,...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |