Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
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
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)

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
 
Share this answer
 
v5
Comments
fjdiewornncalwe 13-Dec-11 12:17pm    
Well explained. +5
Sergey Alexandrovich Kryukov 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!
Sergey Alexandrovich Kryukov 13-Dec-11 12:35pm    
Thank you, Abhinav.
--SA
Monjurul Habib 13-Dec-11 12:56pm    
5!
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.
C#
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#?
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 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 :)
I searched and came across this question[^] asked a few months ago. Should help you out.
 
Share this answer
 
Comments
Monjurul Habib 13-Dec-11 12:57pm    
yes :) 5!
Abhinav S 13-Dec-11 13:03pm    
Thanks. :)
[no name] 13-Dec-11 20:51pm    
5!
Abhinav S 14-Dec-11 0:15am    
Thank you.
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
 
Share this answer
 
v6
Comments
Sergey Alexandrovich Kryukov 13-Dec-11 20:17pm    
Ha! Well said. My 5.
--SA
[no name] 23-Dec-11 12:41pm    
thx. Regards
[no name] 13-Dec-11 20:51pm    
5!
[no name] 23-Dec-11 12:41pm    
thx. Regards
using ref keyword requires that the variable be initialized before being passed
out-no need to initialize before passing to function
 
Share this answer
 
Comments
Manfred Rudolf 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900