Click here to Skip to main content
13,866,049 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

22.4K views
18 bookmarked
Posted 4 Sep 2015
Licenced CPOL

C# Out VS Ref

, 4 Sep 2015
Rate this:
Please Sign up or sign in to vote.
This blog discusses about C# Out vs Ref keyword.
Lot of times we come across C# fundamentals which are bit confusing and one such concept which is discussed a lot is Out VS Ref. So this small blogpost would make a full attempt to simplify the OUT vs REF concept.

Out and Ref are keywords which you need to worry when you want to get MULTIPLE values from a function. It helps you to define how data would be passed from caller to the callee and vice versa.

For example take the case of a simple “Add” function below. It has only one output so with just a single return call things work perfectly well.
 
static int Add(int num1,int num2)
{
      return num1 + num2;
}

But now lets do something more complicated. In the below function we need to get 4 outputs or putting in other words we have four OUT parameter values. This is where OUT keyword comes to help.
 
static int Maths(int num1,int num2)
{
int Add = num1 + num2;
int Sub = num1 - num2;
int Divide = num1 / num2;
int Multi = num1 * num2;
}

When you use OUT keyword you are indicating that you are expecting output from the function but you do not intend to pass data to the function. It is just ONE WAY :- so the Caller get data from the Callee.

So to indicate this one-way data transfer you need to use the OUT keyword on the Callee as shown below.
 
static void Maths(int num1,
int num2,
out int Add,
out int Sub,
out int Multi,
out int Div)
{
             Add = num1 + num2;
             Sub = num1 - num2;
            Div = num1 / num2;
            Multi = num1 * num2;
}

The caller also needs to provide the variables in which the data will be received and the caller needs to mark the variables with OUT keyword while making the call. In short both the caller and callee will have the OUT keyword indicating that data will come out of these variables.
 
int Add,Sub,Multi,Div=0;
Maths(10, 10,out Add,
      out Sub,
      out Multi,
out Div);

Note :- If you are specifying a variable as OUT and also trying to set data while calling, the passed DATA WILL BE DISCARDED inside the method / function.

Now let us take a scenario of a swapping number function code as shown below. In this scenario we want that the function should take “num1” and “num2” values and update the swapped values in the same two variables.
 
static void Swap(int num1, int num2)
{
            num1 = num1 + num2;
            num2 = num1 - num2;
            num1 = num1 - num2;
}

For that we need use the REF keyword as shown below.
 
static void Swap(ref int num1,ref int num2)
{
            num1 = num1 + num2;
            num2 = num1 - num2;
            num1 = num1 - num2;
}

From the caller side we need to again use the REF keyword as shown below.
 
int num1 = 10;
int num2 = 20;
Swap(ref num1,ref  num2);

In REF the data will be passed to the function / method and when the callee method updates the variables it will update the caller variables as well.
 

Below is a visual diagram which explains the difference between REF and OUT. In REF Parameter + Data is passed while in OUT only parameter is passed and data is set from the callee.


So the conclusion is as follows of the above post is as follows:-
  • It should be used only when we are expecting multiple outputs from a function or a method.
  • REF and OUT are keywordswhich dictate how data is passed from caller to callee and vice versa.
REF
OUT
Data passes two way. From caller to callee and vice-versa. Data passes only one way from callee to caller. Caller data if passed is rejected.

Below is a nice C# video which explain Out Vs Ref

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


You may also be interested in...

Pro

Comments and Discussions

 
QuestionNot an article... Pin
0x01AA1-Mar-17 10:34
professional0x01AA1-Mar-17 10:34 
QuestionVery Clear Pin
frikrishna15-May-16 11:51
memberfrikrishna15-May-16 11:51 
QuestionVery Clear Pin
frikrishna15-May-16 11:51
memberfrikrishna15-May-16 11:51 
GeneralAnd one more thing Pin
Emre Ataseven4-Sep-15 7:29
professionalEmre Ataseven4-Sep-15 7:29 
QuestionLittle more explanation on Ref and Out in C# Pin
Ravindranath_Kanojiya4-Sep-15 7:01
professionalRavindranath_Kanojiya4-Sep-15 7:01 
Questionref doesn't pass data, it passes references Pin
PureNsanity4-Sep-15 6:09
professionalPureNsanity4-Sep-15 6:09 
AnswerRe: ref doesn't pass data, it passes references Pin
James Jensen27-Jul-16 12:53
professionalJames Jensen27-Jul-16 12:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190214.1 | Last Updated 4 Sep 2015
Article Copyright 2015 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid