[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Diffrence between REF ans OUT variable in C#?

Posted on 2004-08-26
3
Medium Priority
?
918 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

656 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