Monday, September 28, 2015

Out and REF in C#

Out and REF are one of those most confused topics in C#. In this small article we will try to simplify the same.

There are three important points to remember about OUT and REFand once you understand these three points you would never have problem with OUT and REF.

Point number 1:- By default variables are passed BYVAL to methods and functions. Out and Ref helps to pass variable BY REF (By reference) .
Let us try to understand the above point. Consider the below code snapshot where we have the “Main” method and its calling “SomeFunction” method and its passing “OutSideVar” variable to “SomeFunction”.

Now by default only value of “OutSideVar” will be copied to the method so any changes in “InsideVar” variable change will have no affect outside. You can see after the function call “OutSideVar” still has value “20”. In other words by default the values are passed BY VAL.

So if we summarize in a normal situation data is passed BY VAL , from the caller to callee. Any modifications done in the callee will not be reflected to the caller.

Point number 2:- When we mark parameters with REF, reference of the variable is passed to the function / method and any modification in that method and function will affect to the outside variable.
In the below code you can see we have marked variablesusing the REF keyword and you can see how the changes within the function modifies the outside variable also. This is happening because now variable is passed by reference.

Summarizing in this scenario DATA + REFERENCE is passed to the function and any modification inside the function affects the outside variable.

Point number 3:- When we mark parameters with OUT, ONLY reference of the variable is passed to the function or method and NOT DATA.Any modification in that method and function will affect outside variable as well. Also its compulsory to initialize the variable.

When we mark the variable as OUT only reference is passed no data is passed to the function. For instance in the below code the value “20” from “OutSideVar” is not sent to the function. But any modification in variable from the inside function is reflected to the outside variable.

Summarizing, you can term this also has one way but it’s one way from the callee to the caller and any data sent from the caller is discarded.

So summarizing what we have learnt:-
  • Out and Ref help to pass variables by reference.
  • In REF with reference data is also passed from caller to the callee and vice versa. It’s two way i.e. from caller to the callee and vice-versa.
  • In OUT only reference is passed and modified data from the callee is passed to the caller variable. This is one way from the callee to the caller.
  • In OUT if data is passed from the caller function it is discarded and the variable has to be initialized inside the function.
Below image summarizes what we talked in the above article.

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

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.