Click here to Skip to main content
12,953,865 members (45,210 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
what is the difference between them?
when or where we should use OUT and REF?
Programming example?
RealWorld Example
..
thank you.
Positive discussion is appreciated.
Posted 13-Dec-11 4:55am
Comments
Philippe Mori 13-Dec-11 20:19pm
   
By the way, the compiler documentation is quite adequate also:
out parameter modifier and ref (C# Reference)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

They are almost the same and their low-level implementations are identical; the only difference is the syntax requirements to the implementation of the method.

If the argument is "out", the compiler enforces assignment of a new value to the parameter. More exactly, if a compiler can see the situation where this parameter is left unassigned, it generates a compilation error. This is a powerful fool-proof feature.

The "ref" argument impose more relaxed rules which is adequate to "in-out" behavior. Modification of the value of this parameter is not required. If the value is not modifies on return, it's fine, the value will remain the same as before the call.

[EDIT] I forgot to mention that "out" variable does not have to be initialized. The requirement I mentioned above allows to consider such variable as initialized after the call and guarantee initialization. Of course "ref" parameter is required to be initialized. Credit to Monjurul.

—SA
  Permalink  
v5
Comments
Marcus Kramer 13-Dec-11 12:17pm
   
Well explained. +5
SAKryukov 13-Dec-11 12:28pm
   
It was easy, in contrast to many other cases. :-)
Thank you, Marcus.
--SA
Abhinav S 13-Dec-11 12:34pm
   
My 5!
SAKryukov 13-Dec-11 12:35pm
   
Thank you, Abhinav.
--SA
Monjurul Habib 13-Dec-11 12:56pm
   
5!
SAKryukov 13-Dec-11 13:04pm
   
Thank you, Monjurul; and thanks again for helping to correct this answer.
--SA
Monjurul Habib 13-Dec-11 13:06pm
   
anytime, my pleasure
RaviRanjankr 13-Dec-11 16:10pm
   
My 5 for nice answer :)
SAKryukov 13-Dec-11 16:17pm
   
Thank you, Ravi.
--SA
Wonde Tadesse 13-Dec-11 19:38pm
   
5+
SAKryukov 13-Dec-11 20:16pm
   
Thank you, Wonde.
--SA
Eduard Lu 13-Dec-11 20:50pm
   
5!
SAKryukov 13-Dec-11 21:26pm
   
Thank you, Eduard.
--SA
RakBallal 2-Mar-13 3:58am
   
Well explained..thank U
   
Thank you.
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Both are pretty similar - the only difference is that a variable you pass as an out parameter doesn't need to be initialized, and the method using the out parameter has to set it to something.
int a;
Item(out a); // OK
 
int b;
Item(ref b); // Not Ok


Ref parameters are for data that might be modified, out parameters are for data that's an additional output for the function (eg int.TryParse) that are already using the return value for something.

For more detail information please visit the following link:
The out and ref Paramerter in C#
What is the difference between out and ref in C#?
  Permalink  
v3
Comments
SAKryukov 13-Dec-11 12:32pm
   
Oh gosh, I forget to mention about initialization, but now I fixed my answer, with some extra explanations.
Thank you and my 5. My credit for your answer is in my updated solution.
--SA
Monjurul Habib 13-Dec-11 12:55pm
   
thank you
Abhinav S 13-Dec-11 12:34pm
   
Useful to the OP. 5.
Monjurul Habib 13-Dec-11 12:55pm
   
thank you
Uday P.Singh 13-Dec-11 12:49pm
   
5ed :)
Monjurul Habib 13-Dec-11 12:55pm
   
thank you
RaviRanjankr 13-Dec-11 16:10pm
   
My 5!
Monjurul Habib 13-Dec-11 22:44pm
   
thank you
Wonde Tadesse 13-Dec-11 18:09pm
   
5+
Monjurul Habib 13-Dec-11 22:44pm
   
thank you
Eduard Lu 13-Dec-11 20:50pm
   
5!
Monjurul Habib 13-Dec-11 22:45pm
   
thank you
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

I searched and came across this question[^] asked a few months ago. Should help you out.
  Permalink  
Comments
Monjurul Habib 13-Dec-11 12:57pm
   
yes :) 5!
Abhinav S 13-Dec-11 13:03pm
   
Thanks. :)
Eduard Lu 13-Dec-11 20:51pm
   
5!
Abhinav S 14-Dec-11 0:15am
   
Thank you.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Only to become a feeling: Before a long time with old programming languages there was something like passing parameters by reference (often misused to avoid overhead when passing by value). But nobody checked really whether the parameter(s) has been set by the called method.

So with the “out parameter” some unknowns are removed at compile time, because it guarantees that the called method has to assign something to these parameters.

And by the way: Removing uncertainty means Information ;)

Regards
  Permalink  
v6
Comments
SAKryukov 13-Dec-11 20:17pm
   
Ha! Well said. My 5.
--SA
tambores63 23-Dec-11 12:41pm
   
thx. Regards
Eduard Lu 13-Dec-11 20:51pm
   
5!
tambores63 23-Dec-11 12:41pm
   
thx. Regards
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

using ref keyword requires that the variable be initialized before being passed
out-no need to initialize before passing to function
  Permalink  
Comments
Manfred R. Bihy 13-Jan-13 14:49pm
   
What has your solution added that all the others didn't already say?
Reported as abuse!
jibesh 13-Jan-13 16:27pm
   
also why you answered for the question which a year old and its already solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,429
CHill60 3,490
Maciej Los 3,103
ppolymorphe 2,020
Jochen Arndt 1,975


Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100