Ref And Out KeyWords In C#

Ref and Out are the keywords used for passing variable by reference into function. Both are rarely used in programming, but we cannot ignore these two keywords. By default, the function parameters are value type. Either we pass these as object type or primitive type.
 
Ref and Out both are also useful for returning multiple values or output by a function. I will give an example later in this article. By default, all the functions return a single value that can either be object type or array type. But sometimes, it is necessary to have multiple output, and these keywords make us return multiple output.
 
Suppose, I am creating a simple calculator for getting the sum and difference of two numbers, but I don’t want to create two separate functions - one for sum and the other one for difference. So, I decided to create a function that returns both the values. This is possible by using Ref and Out keywords.
 
Before working with Ref and Out keywords, we must take care of the following things.
  1. Ref and Out, both are used for passing parameters by reference into a function.

  2. Ref is by-directional because it can pass the value from caller to calling function parameter and after performing the operation on this variable, calling function returns the value back to the caller function.

  3. Out is single-directional because the value which we pass into calling function parameter is always being discarded. So, in case of Out, it does not matter what we are passing.

  4. In case of Out, it is necessary to initialize the variables inside the calling function, otherwise it will give the "compile time error".

  5. Out and Ref work differently at run time but both are same at compile time. So, if you want to create two functions with the same names and same parameters but the only difference is Out and Ref keyword, then it will give a compile time error. We can say overloading is not possible in case of  the same functions and they have only one difference, i.e. Ref and Out keyword
Requirement of Ref and Out
  1. Sometimes, in the variable of my caller function, we want to reflect all the changes we made in the calling function. Then, we need to use Ref and Out keyword.

  2. Sometimes, we want to return multiple values from a function or method, then we think about Ref and Out.
Passing parameter by value
 
I created a functionWithRef function with one int parameter Num. I initialize Num with 10 before passing it into function. Here Main function is caller and functionWithRef is calling. After passing the value in calling function the value of Num is 14 but when we exit from this function value of Num in the caller function is still 10. Now it is clearly described that when we pass parameter by value it creates a separate copy of that variable and performs all operations and after exiting the function body it erases all the copied variable memory.

Value inside the calling function
 


Value inside the caller function.



So if we want to reflect all changes on variable inside the caller function which have been done in calling function variable then we use out and ref keyword.

Now we modify the same function and use the ref Keyword with Num variable let’s see what happens.
 
Passing parameter by ref
 
I changed the Num parameter to ref Num parameter then called this function from caller function. I assigned 10 to Num and passing it into functionWithRef then debugged application, the value we passed inside the function and added 4 with this value, now the value of Num is 14 and after exiting the calling function, same value still remains in variable of caller function.
 
Value inside the calling function.

 
Value inside the caller function.

 
So ref is basically bi directional; it can take value from caller function and apply operation on this variable inside the calling function and revert back to the caller function with updated value  -- you can see the difference in given screen shots.
 
Passing parameter by Out
  
I created a new function functionWithout with ‘out Num’ parameter then called this function from caller function. I assigned 10 to Num and passed it into functionWithout then debugged application, the value we passed inside the function has been discarded by function and we need to re-initialize inside the function with 0 (any value) and add 4 with this value, now the value of Num is 4 and after exiting the calling function, same value still remains in variable of caller function.

In case of Out, no matter what we are passing with parameter in calling function, it is always discarded. So, it is a compulsion to assign the value to this variable inside the calling function. If we do not do this, it will show the compile time error.
 
Value inside the calling function.



Value inside the caller function.

 
If we do not assign the value inside the calling function then it will give the compile time error you can see in given screen shot.
 

 
If we create the same name function and both have same parameters, but the only difference is Ref and Out, parameter one will have Ref and the other will have Out, then it will show compile time error as shown below.
 

 
Returning multiple value from a function using ref
 
Ref and out both are use for returning multiple values from function. Here I am creating a function calculator that has four parameters, FirstNumber , SecondNumber, ref int Sum and ref int difference. By default this function returns the multiplication of two numbers but we will return sum and difference by ref parameter.

You can see in the given example that the calculator function returns multiplication of two variables but the sum and difference of these two variable are assigned to the variable which are passed with reference.

Thus it clearly seems that this function returns three output multiplications, sum and difference. Out also works like this.


Ebook Download
View all
Learn
View all