Solved

Diffrence between REF ans OUT variable in C#?

Posted on 2004-08-26
3
887 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 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now