Thursday, September 3, 2015

C# Out VS Ref

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.
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 one such concept which is not known to many developers, the C# yield keyword. Below is a simple video of the same which explains the same in a practical manner.

No comments: