[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Diffrence between REF ans OUT variable in C#?

Posted on 2004-08-26
3
Medium Priority
?
921 Views
Last Modified: 2011-10-03
He guys can any one please deifne to me the major diffrance between REF and OUT variables. and why are they distinguished? the only diffrance i know is that OUT variable is unassigned, but why? i mean what good it can be? can any one please give some examples to help me this question which is bugging me?
0
Comment
Question by:manan34
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
dimitry_dimitry earned 500 total points
ID: 11910368
Have you read MSDN on it? Some extracts:
Like all out parameters, an out parameter of an array type must be assigned before it is used; that is, it must be assigned by the callee.
Like all ref parameters, a ref parameter of an array type must be definitely assigned by the caller. Therefore, there is no need to be definitely assigned by the callee. A ref parameter of an array type may be altered as a result of the call. For example, the array can be assigned the null value or can be initialized to a different array.
The out method parameter keyword on a method parameter causes a method to refer to the same variable that was passed into the method. Any changes made to the parameter in the method will be reflected in that variable when control passes back to the calling method.
Declaring an out method is useful when you want a method to return multiple values. A method that uses an out parameter can still return a value. A method can have more than one out parameter.
A variable passed as an out argument need not be initialized. However, the out parameter must be assigned a value before the method returns.
A property is not a variable and cannot be passed as an out parameter.

Now ref:
The ref method parameter keyword on a method parameter causes a method to refer to the same variable that was passed into the method. Any changes made to the parameter in the method will be reflected in that variable when control passes back to the calling method.
To use a ref parameter, the argument must explicitly be passed to the method as a ref argument. The value of a ref argument will be passed to the ref parameter.
An argument passed to a ref parameter must first be initialized. Compare this to an out parameter, whose argument does not have to be explicitly initialized before being passed to an out parameter.

I see 2 major differences:
1) Initialization
2) Design issues - using out tells users that method returns multiple values, instead of ref.
0
 

Author Comment

by:manan34
ID: 11910416
really great answers i am now cleared about the concept of OUT and REF. basically i think the main purpose of OUT is to return 2 Variables from one method.

but thanx any why great answer.
0
 
LVL 2

Expert Comment

by:dimitry_dimitry
ID: 11910438
With great pleasure;)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Progress

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question