Author : Rahul
Last Modified : 22-May-2021
Complexity : Beginner

Ref and Out keyword in C#


There is two way to pass the parameter to a method. One is the value type and, the other is the reference type. In the value type, The value of the variable is passed, and in the reference type, the reference of the variable is passed. To pass the parameter by reference Ref and Out keyword is used.  When we change the value of the reference variable, this value is not changed locally instead this value is changed globally because we are working on the address. Below is the detailed explanation of Ref and Out keywords.

Ref

To pass a parameter by reference, the ref keyword is used. If the value of the parameter is changed in the called method and this parameter is passed using the ref keyword, this value will also be changed in the calling method. The parameter which is passed using the ref keyword needs to give some initial value before passing to the called method. So ref parameters are bi-directional. They pass the values from calling method to called method and from called method to calling method.

Example

using System; 
 
 class Test
 {
   public static void Main()
   {
     string str = "Rahul";
     SetValue(ref str);
     Console.WriteLine(str);
   }
   private static void SetValue(ref string str1)
   {
     // Check parameter value 
     if (str1 == "Rahul")
     {
       Console.WriteLine("Hello!!Rahul");
     }
     // Assign new value  
     str1 = "Rahul Goel";
   }
 }

Output

Hello!!Rahul
Rahul Goel

Out

To pass a parameter by reference, the out keyword is also used. If the value of the parameter is changed in the called method and this parameter is passed using the out keyword, this value will also be changed in the calling method. The parameter which is passed using the out keyword does not require giving some initial value before passing to the called method. So out parameters are uni-directional. They pass the values from called method to the calling method. So when we want to return multiple values from a called method, the out keyword is used.

Example

using System; 
 
 class Test
 {
   public static void Main()
   {
     int val;
     Increment(out val);
     Console.WriteLine("The new value of val is: {0}", val);
   }
   public static void Increment(out int val1)
   {
     val1 = 80;
     val1 = val1 + 10;
   }
 }

Output:

The new value of val is: 90


Method Overloading with Ref/Out parameter

Method overloading is not possible with the ref and out parameter because both are the same at compile time meaning that at compile time both parameters are used to pass the reference of the variable.

Example

using System; 
 
 class Test
 {
   public static void Main()
   {
     int id = 10;
     string val1 = GetStringValue(ref id);
     string val2 = GetStringValue(out id);
     Console.WriteLine("The value of val1: {0}", val1);
     Console.WriteLine("The value of val2: {0}", val2);
   }
   public static string GetStringValue(ref int id)
   {
     string returnValue = id.ToString();
     id += 1;
     return returnValue;
   }
   public static string GetStringValue(out int id)
   {
     id = 1;
     string returnValue = id.ToString();
     return returnValue;
   }
 }

Output

Error message:
Cannot define overloaded method "GetStringValue" because it differs from another method only on ref and out.

Method overloading is only possible with the ref/out parameter if for the same variable, one method is used the ref/out parameter and the other method is not used the ref/out parameter. 

Example

using System; 
 
 class Test
 {
   public static void Main()
   {
     int id = 10;
     string val1 = GetStringValue(id);
     Console.WriteLine("The value of val1: {0}", val1);
     Console.WriteLine("The value of Id: {0}", id);
     string val2 = GetStringValue(ref id);
     Console.WriteLine("The value of val2: {0}", val2);
     Console.WriteLine("The value of Id: {0}", id);
   }
   public static string GetStringValue(int id)
   {
     string returnValue = id.ToString();
     id += 1;
     return returnValue;
   }
   public static string GetStringValue(ref int id)
   {
     string returnValue = id.ToString();
     id += 1;
     return returnValue;
   }
 }

Output:

The value of val1: 10
The value of Id: 10
The value of val2: 10
The value of Id:11

Below are some differences between ref and out keyword in C#

Ref

Out

Ref keyword required to give some initial values before passing to the called method.Out keyword does not require giving some initial values before passing to the called method.
Ref keyword does not require assigning some values into it before passing back to the calling method.Out keyword required to assign some values into it before passing back to the calling method.
Ref parameters are bi-directional. They pass the values from calling method to called method and from called method to calling method.Out parameters are uni-directional. They pass the values from called method to the calling method.
It is not required to give some initial value before using it in the called method.It is required to give some initial value before using it in the called method.