Ref and Out Parameters in C#

Background

by value is the default behavior of C# methods but we can change that by ing parameters by reference. That does not create any new storage location, so let us start from the basics so beginners can also understand this concept.

What the ref parameter in C# is
 
The ref keyword is used to the parameters to a method by a reference in C#, the parameter ed using the ref keyword does not create a new storage location that is created by a normal value parameter.
 
When we a parameter by reference then the formal argument in the called method becomes an alias to the actual element in the calling method, in other words when you the value by reference, you actually the address of that value, not a copy of that value.
 
The following example demonstrates this concept.
 
  1. using System;  
  2.   
  3. namespace Usingrefandoutparam  
  4. {  
  5.     class Program  
  6.     {  
  7.         public void Multiplication(ref int a)  
  8.         {  
  9.   
  10.           int d=a* a;  
  11.           Console.WriteLine("Multiplication of C*C:"+" "+d);  
  12.           
  13.         }  
  14.         static void Main(string[] args)  
  15.         {  
  16.             Program p = new Program();  
  17.             int c = 10;  
  18.             Console.WriteLine();  
  19.             Console.WriteLine("Value of C before ing C reference into ref method:"+" "+c);  
  20.             Console.WriteLine();  
  21.             p.Multiplication(ref c);  
  22.             Console.WriteLine();  
  23.             Console.WriteLine("Value of C after ing C reference into ref method:" + " " + c);  
  24.             Console.ReadLine();  
  25.         }  
  26.     }  
  27. }  
In the preceding program we are ing the value to the multiplication method by reference instead of direct value.
Now run the program and see the output.
 
 
 
 What the Out  parameter in C# is
 
The out parameter will return the result back to the calling method, similar to the return type but by using the ref parameter the result is ed in an out parameter without creating the new storage location.

When a formal parameter is declared as out, then the corresponding actual parameter in the calling method must be declared as out, the out parameter is useful when our method does not return anything and we want something to be get back a result from that method.

Let us see the following example that will demonstrate this concept.

  1. using System;  
  2.   
  3. namespace Usingrefandoutparam  
  4. {  
  5.     class Program  
  6.     {  
  7.         public void Getresult(int a,out int b)  
  8.         {  
  9.   
  10.           b=a+a; //additin result is stored into the b ouput parameter  
  11.          
  12.           
  13.         }  
  14.   
  15.         
  16.         static void Main(string[] args)  
  17.         {  
  18.             Program p = new Program();  
  19.             int Result;  
  20.             p.Getresult(100, out Result);  
  21.              
  22.             Console.WriteLine(Result); //getting the result using Result from b out parameter  
  23.             Console.ReadLine();  
  24.         }  
  25.     }  

In the preceding program we declared one out parameter named b in the Getresult method that stores the result of the addition of two numbers and in the calling method we are ing the values and accessing  the result in the Result variable that is stored into the b out variable.
Now run the program, the output will be as follows:
 

Summary

I hope this article is useful for interview prospects. If you have any suggestion regarding this article then please contact me.

Up Next
    Ebook Download
    View all
    Learn
    View all