Ref And Out keyword In C#

Ref

Ref is used for returning value from method. It is used with method parameters and returns the same variable value that was passed in the method as parameter. When you pass ref parameter in the method, it must initialize the value of a variable.

Example 1

  1. public class Program  
  2. {  
  3.   
  4.     public void GetNumber(ref int i)  
  5.     {  
  6.         i = i + 10;  
  7.     }  
  8.     public static void Main(string[] args)  
  9.     {  
  10.         int firstnumber;  //must be initialize first before use           
  11.   
  12.         Program objProgram = new Program();  
  13.         objProgram.GetNumber(ref firstnumber);  
  14.         Console.WriteLine("The value is " + firstnumber);  
  15.   
  16.   
  17.         Console.ReadLine();  
  18.   
  19.     }  
  20. }  
See the above example, when you run the project, you will get an error, which clearly says that you are using unassigned local variable. That means you need to initialize the value of variable.

error

Example 2
  1. public class Program  
  2. {  
  3.     public void GetNumber(ref int i)  
  4.     {  
  5.         i = i + 10;  
  6.     }  
  7.     public static void Main(string[] args)  
  8.     {  
  9.         int firstnumber; // must be initialize first before  
  10.         firstnumber = 10;  
  11.   
  12.         Program objProgram = new Program();  
  13.         objProgram.GetNumber(ref firstnumber);  
  14.   
  15.         Console.WriteLine("The value is " + firstnumber);  
  16.         Console.ReadLine();  
  17.   
  18.     }  
  19. }  
When you run the above program after initializing the value of first number, then it will run without any error and output should be like the following:

cmd

Out

Out is only something different from Ref. It is necessary to be initialized before the use of out parameter in the method. It is used with method parameters and returns the same variable value that was passed in the method as parameter. You can pass multiple out parameter inside the method.

Note: If you made any changes on the time of calling the function in arguments then it will reflect on the variables.

Example 3
  1. public class Program  
  2. {  
  3.     public void GetNumber(out int i)  
  4.     {  
  5.         i = 20;  
  6.         i = i + 10;  
  7.     }  
  8.     public static void Main(string[] args)  
  9.     {  
  10.         int firstnumber;  
  11.         firstnumber = 10; //there is no need to initialize, you can initialize inside the method also.   
  12.   
  13.   
  14.         Program objProgram = new Program();  
  15.         objProgram.GetNumber(out firstnumber);  
  16.   
  17.         Console.WriteLine("The value is " + firstnumber);  
  18.   
  19.   
  20.         Console.ReadLine();  
  21.   
  22.     }  
  23. }  
See the above example you will find that the value of out parameter has been changed inside the method. So the output value will be 30.

output

Thanks for reading this article, I hope you enjoyed it.

 


Similar Articles