Solved

Diffrence between REF ans OUT variable in C#?

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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…
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…
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

831 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