Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# programming
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 5:55am
Comments
Philippe Mori at 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 at 13-Dec-11 12:17pm
   
Well explained. +5
SAKryukov at 13-Dec-11 12:28pm
   
It was easy, in contrast to many other cases. :-)
Thank you, Marcus.
--SA
Abhinav S at 13-Dec-11 12:34pm
   
My 5!
SAKryukov at 13-Dec-11 12:35pm
   
Thank you, Abhinav.
--SA
Monjurul Habib at 13-Dec-11 12:56pm
   
5!
SAKryukov at 13-Dec-11 13:04pm
   
Thank you, Monjurul; and thanks again for helping to correct this answer.
--SA
Monjurul Habib at 13-Dec-11 13:06pm
   
anytime, my pleasure
RaviRanjankr at 13-Dec-11 16:10pm
   
My 5 for nice answer :)
SAKryukov at 13-Dec-11 16:17pm
   
Thank you, Ravi.
--SA
Wonde Tadesse at 13-Dec-11 19:38pm
   
5+
SAKryukov at 13-Dec-11 20:16pm
   
Thank you, Wonde.
--SA
Eduard Lu at 13-Dec-11 20:50pm
   
5!
SAKryukov at 13-Dec-11 21:26pm
   
Thank you, Eduard.
--SA
RakBallal at 2-Mar-13 3:58am
   
Well explained..thank U
Sergey Alexandrovich Kryukov at 2-Mar-13 20:09pm
   
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 at 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 at 13-Dec-11 12:55pm
   
thank you
Abhinav S at 13-Dec-11 12:34pm
   
Useful to the OP. 5.
Monjurul Habib at 13-Dec-11 12:55pm
   
thank you
Uday P.Singh at 13-Dec-11 12:49pm
   
5ed :)
Monjurul Habib at 13-Dec-11 12:55pm
   
thank you
RaviRanjankr at 13-Dec-11 16:10pm
   
My 5!
Monjurul Habib at 13-Dec-11 22:44pm
   
thank you
Wonde Tadesse at 13-Dec-11 18:09pm
   
5+
Monjurul Habib at 13-Dec-11 22:44pm
   
thank you
Eduard Lu at 13-Dec-11 20:50pm
   
5!
Monjurul Habib at 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 at 13-Dec-11 12:57pm
   
yes :) 5!
Abhinav S at 13-Dec-11 13:03pm
   
Thanks. :)
Eduard Lu at 13-Dec-11 20:51pm
   
5!
Abhinav S at 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 Wink | ;)
 
Regards
  Permalink  
v6
Comments
SAKryukov at 13-Dec-11 20:17pm
   
Ha! Well said. My 5.
--SA
tambores63 at 23-Dec-11 12:41pm
   
thx. Regards
Eduard Lu at 13-Dec-11 20:51pm
   
5!
tambores63 at 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 at 13-Jan-13 14:49pm
   
What has your solution added that all the others didn't already say?
Reported as abuse!
jibesh at 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
0 Zoltán Zörgő 220
1 Sergey Alexandrovich Kryukov 180
2 Peter Leow 160
3 BillWoodruff 85
4 bling 80
0 Sergey Alexandrovich Kryukov 9,543
1 OriginalGriff 6,901
2 Peter Leow 4,737
3 Zoltán Zörgő 4,404
4 CHill60 2,932


Advertise | Privacy | Mobile
Web04 | 2.8.150129.1 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2015
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