Ref Vs Out C#

Introduction

 
Ref and Out are two keywords in C# which give us an opportunity to pass a variable in methods by reference.
 
Both are treated differently at run-time but they are treated the same at compile time.
 
Add a Class and implement the following code:
  1. class Class3  
  2. {  
  3.    //Callee  
  4.    static void Method1(int variable1)  
  5.    {  
  6.       variable1 = variable1 + 60;  
  7.    }  
  8.    //Caller  
  9.    public static void Main()  
  10.    {  
  11.       int variable2 = 40;  
  12.       Method1(variable2);  
  13.       Console.WriteLine(variable2);  
  14.       Console.ReadLine();  
  15.    }  
  16. }  
O/P - 40
 
Note
Whatever changes happened to variable1 in Method1() it is not going to reflect variable2. Hence, we could say that the data is passed by value.
 
Ref
 
Modify the class and use the ref keyword to pass by reference. In the below example, we can see that when we are working with variable1, we are actually working with variable2.
  1. class Class3  
  2. {  
  3.    //Callee  
  4.    static void Method1(ref int variable1)  
  5.    {  
  6.       variable1 = variable1 + 60;  
  7.    }  
  8.    //Caller  
  9.     public static void Main()  
  10.    {  
  11.       int variable2 = 40;  
  12.       Method1(ref variable2);  
  13.       Console.WriteLine(variable2);  
  14.       Console.ReadLine();  
  15.    }  
  16. }  
O/P - 100
 
Note
Ref is always a Two way from Caller to Callee and Callee to Caller.
 
Out
 
It also passes by reference but we need to initialize variable1 within Method1() because even if we pass data from outside the function, it will still be discarded.
  1. class Class3  
  2. {  
  3.    //Callee  
  4.    static void Method1(out int variable1)  
  5.    {  
  6.       variable1 = 30;  
  7.       variable1 = variable1 + 60;  
  8.    }  
  9.    //Caller  
  10.    public static void Main()  
  11.    {  
  12.       int variable2 = 40;  
  13.       Method1(out variable2);  
  14.       Console.WriteLine(variable2);  
  15.       Console.ReadLine();  
  16.    }  
  17. }  
O/P - 90
 
Note
Out is always one way that sends back data from Callee to Caller
 
So, when do we use what?
 
If we have to return more than one value from a method, we would go for the out keyword.The below example demonstrates the exact usage where we are actually getting two results using the out keyword. Check out the Add_Sub() method below.
 
Code
  1. //Callee  
  2. static void Add_Sub(int x, int y, out int add_result, out int sub_result)  
  3. {  
  4.    add_result = x + y;  
  5.    sub_result = x - y;  
  6. }  
  7. //Caller  
  8. public static void Main()  
  9. {  
  10.    int result_add ;  
  11.    int result_sub;  
  12.    Add_Sub(20,20,out result_add,out result_sub);  
  13.    Console.WriteLine("Add : " + result_add);  
  14.    Console.WriteLine("Sub : " + result_sub);  
  15.    Console.ReadLine();  
  16. }  
 
As a suggestion, beginners should make use of the breakpoints to debug and understand the concept...
 
Happy Learning... 


Next Recommended Reading Ref Keyword in C#