What is difference b/w OUT and REF keyword
Hi can any one tell me what is difference b/w out and ref keyword and why and where used and which one is used for reference type and which one is uesd for value type.............?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Apr 27, 2012, 11:16 AM EDT
The difference between them is that:
A 'ref' parameter needs to have been assigned a value before it's passed to the method.
An 'out' parameter does not need to have been assigned a value before it's passed to the method but it must be assigned a value before the method returns. It is commonly used to return an additional value from a method to the return value itself.
'ref' and 'out' can be applied to either value type or reference type parameters.
In the case of value types, this means that a variable passed in as a 'ref' parameter may contain a different value when the method returns than it did when the method was called.
In the case of reference types, it means that a variable passed in as a 'ref' parameter may refer to a different object when the method returns than it did when the method was called. It could also refer to the same object whose state has been changed by the method.
For more details and some examples, I'd check our recommended articles in the sidebar to the right of this thread.
MuthuMari MRank #5,738 · 288 reputationPosted Apr 28, 2012, 9:36 AM EDT
The out Keyword lets you pass an argument to a method as a reference. You need to use the out keyword explicitly both at the method definition and at the method calling, out keyword is very similar to ref as both get passed as reference but the difference is that out need not be initialized before being passed to a method.
The ref keyword lets you pass arguments by reference, so if you make any changes to the passed parameter in the method the changes are reflected in the original variable which was passed as an argument.
pls refer this url for ex
http://dotnetrobert.com/?q=node/209
OUT will be used when you want data to be passed only from the method to the caller.
REF will be used when you want data to be passed from the called to the method and also vice versa.
thanks.
If this post is useful then mark it as "Accepted Answer"