Click here to Skip to main content
15,867,771 members
Articles / Programming Languages / C#

C# Out VS Ref

Rate me:
Please Sign up or sign in to vote.
5.00/5 (21 votes)
4 Sep 2015CPOL3 min read 41.4K   19   10
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.
 
Image 1

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

Image 2

For Further reading do watch  the below interview preparation videos and step by step video series.

License

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


Written By
Architect https://www.questpond.com
India India

Comments and Discussions

 
QuestionThanking about REF and OUT Pin
Member 81846973-Nov-20 5:08
professionalMember 81846973-Nov-20 5:08 
QuestionNot an article... Pin
User 110609791-Mar-17 9:34
User 110609791-Mar-17 9:34 
QuestionVery Clear Pin
frikrishna15-May-16 10:51
frikrishna15-May-16 10:51 
QuestionVery Clear Pin
frikrishna15-May-16 10:51
frikrishna15-May-16 10:51 
GeneralAnd one more thing Pin
Emre Ataseven4-Sep-15 6:29
professionalEmre Ataseven4-Sep-15 6:29 
QuestionLittle more explanation on Ref and Out in C# Pin
Ravindranath_Kanojiya4-Sep-15 6:01
professionalRavindranath_Kanojiya4-Sep-15 6:01 
Questionref doesn't pass data, it passes references Pin
PureNsanity4-Sep-15 5:09
professionalPureNsanity4-Sep-15 5:09 
AnswerRe: ref doesn't pass data, it passes references Pin
James Jensen27-Jul-16 11:53
professionalJames Jensen27-Jul-16 11: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.