Solved

Diffrence between REF ans OUT variable in C#?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 59
nestparen challenge 4 56
groupSum6 challenge 6 38
wordlen challenge 3 47
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
A short article about a problem I had getting the GPS LocationListener working.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

757 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

17 Experts available now in Live!

Get 1:1 Help Now