Ref And Out KeyWords - Two Separated Twins

In this article, I am going to explain the concepts of some useful keywords, like Ref & Out parameters. These keywords are used along with the data to be passed to a function as a parameter. Below, I will explain in brief about each of these keywords with an example.

Ref Keyword

The Ref keyword causes an argument of a function to be passed by reference and not by value. Any change made to the parameters in the called method will reflect its original value. For example, if you pass a local variable, and the call method changes the object to which the ref parameters refer, then the local variable now points to the new object; i.e., in simple words the value got updated to the new value.

It is mandatory to declare and assign a value to the variable which needs to get passed as a ref parameter. Otherwise, it will give a compilation error as below.

 

Programming Example 1: Using Ref Keyword

  1. using System;  
  2. namespace RefOutParams {  
  3.     class RefExample {  
  4.         public void show(int iLocalVar, ref int iRefVar) {  
  5.             iLocalVar += 10;  
  6.             iRefVar += 10;  
  7.         }  
  8.     }  
  9.     class Program {  
  10.         static void Main(string[] args) {  
  11.             int iLocalVar = 10, iRefVar = 10;  
  12.             RefExample objRef = new RefExample();  
  13.             objRef.show(iLocalVar, ref iRefVar);  
  14.             Console.WriteLine(String.Format("Local Variable : {0}", iLocalVar));  
  15.             Console.WriteLine(String.Format("Referenced Variable : {0}", iRefVar));  
  16.         }  
  17.     }  
  18. }  

Output

Local Variable: 10
Referenced Variable: 20

Out Keyword

The Out keyword also causes an argument of a function to be passed by reference only. It is also the same as ref parameter, except that ref requires the variable to be initialized before it is passed to the method, while for out doesn't need initialization of the variable.

Programming Example 2: Using Out Keyword

  1. using System;  
  2. namespace RefOutParams {  
  3.     class OutExample {  
  4.         public void show(ref int iRefVar, out int iLocalVar1, out int iLocalVar2) {  
  5.             iRefVar += 10;  
  6.             iLocalVar1 = 100;  
  7.             iLocalVar2 = 200;  
  8.         }  
  9.     }  
  10.     class Program {  
  11.         static void Main(string[] args) {  
  12.             int iRefVar = 20;  
  13.             int iOutVar1 = 0, iOutVar2;  
  14.             OutExample objOut = new OutExample();  
  15.             objOut.show(ref iRefVar, out iOutVar1, out iOutVar2);  
  16.             Console.WriteLine(String.Format("Referenced Variable: {0}", iRefVar));  
  17.             Console.WriteLine(String.Format("Out Parameter Variable 1: {0}", iOutVar1));  
  18.             Console.WriteLine(String.Format("Out Parameter Variable 2: {0}", iOutVar2));  
  19.         }  
  20.     }  
  21. }  

Output

Referenced Variable: 30
Out Parameter Variable 1: 100
Out Parameter Variable 2: 200

Some points to remember while considering out parameter:

Out parameter should always be assigned a value in call method, otherwise it will throw a compilation error as shown below. In case of ref parameter, though the value is not assigned to it,  it will not throw any compilation error. Have a look at the below image.

 

Out parameter should never be used to assign a value to any other variable as it will throw the compilation error. The reason behind this is that out parameters are allowed to be passed without initialization. Have a look at the below code where the parameter was initialized before getting passed to the method but still shows compilation error.
 
 

Note

From C# 7.0 onwards, there is no need to declare out parameter. It can be passed directly to the call method without declaration. Below is the code snippet for it.

Programming Example 3: Using Out Keyword in with C# 7.0

  1. using System;  
  2. namespace RefOutParams {  
  3.     class OutExample {  
  4.         public void show(ref int iRefVar, out int iLocalVar1, out int iLocalVar2) {  
  5.             iRefVar += 10;  
  6.             iLocalVar1 = 100;  
  7.             iLocalVar2 = 200;  
  8.         }  
  9.     }  
  10.     class Program {  
  11.         static void Main(string[] args) {  
  12.             int iRefVar = 20;  
  13.             int iOutVar1 = 0;  
  14.             OutExample objOut = new OutExample();  
  15.             objOut.show(ref iRefVar, out iOutVar1, out iOutVar2);  
  16.             Console.WriteLine(String.Format("Referenced Variable: {0}", iRefVar));  
  17.             Console.WriteLine(String.Format("Out Parameter Variable 1: {0}", iOutVar1));  
  18.             Console.WriteLine(String.Format("Out Parameter Variable 2: {0}", iOutVar2));  
  19.         }  
  20.     }  
  21. }  

Output

Referenced Variable: 30
Out Parameter Variable 1: 100
Out Parameter Variable 2: 200

Conclusion

Remember, it is mandatory to use ref & out while passing a value to a function. I hope you enjoyed the article.


Similar Articles