Ref, Out, and Optional Parameters in C#

Ref and out parameters are used to pass an argument within a method.

Ref and out will change the behavior of function parameters. Sometimes we want the actual value of a variable to be copied as the parameter. Other times we want a reference. These modifiers affect definite assignment analysis.

The compiler demands that an out parameter be "definitely assigned" before exit from the method. There is no such restriction with the ref type.

Ref

The ref keyword is used to pass an argument as a reference. This means that when value of that parameter is changed in the method, it gets reflected in the calling method. An argument that is passed using a ref keyword must be initialized in the calling method before it is passed to the called method.

  1. public static int square(ref int num)  
  2.         {  
  3.             int result = num * num;  
  4.             num++;  
  5.             return result;  
  6.         }  
  7.         static void Main(string[] args)  
  8.         {  
  9.             int n = 10;  
  10.             Console.WriteLine("square of n value:" + square(ref n));  
  11.             Console.WriteLine("Current value of n: {0}", n);  
  12.         }      

Out

The out keyword is also used to pass an argument like ref keyword, but the argument can be passed without assigning any value to it. An argument that is passed using an out keyword must be initialized in the called method before it returns back to calling method. Usually out keyword is used when user want to return more than one value from the method.

  1. public static void complexMath(int num, out int sqr, out int cube)  
  2.        {  
  3.            sqr = num * num;  
  4.            cube = num * num * num;  
  5.            return;  
  6.        }  
  7.        static void Main(string[] args)  
  8.        {  
  9.            int n = 10;  
  10.            int sqr, cube;  
  11.            complexMath(n, out sqr, out cube);  
  12.            Console.WriteLine("square of n value:{0}", sqr);  
  13.            Console.WriteLine("Cube of n value:{0}", cube);  
  14.        }    

Optional Parameters:

A method with an optional parameter can be called with only some of its parameters specified. Each optional parameter has a default value as part of its definition. If no argument is sent for that parameter, the default value is used.

  1. using System;    
  2.     
  3. class Program    
  4. {    
  5.     static void Main()    
  6.     {       
  7.       printDetails (“Satish”, 23); // prints name : Satish, age : 23    
  8.       // You can omit the age parameter, then age will be 25    
  9.       printDetails (“Satish”);     // prints name : Satish, age : 25    
  10.     }    
  11.     
  12.     static void printDetails(string name, int age = 25)    
  13.     {    
  14.        Console.WriteLine("name = {0}, age = {1}", name, age);    
  15.     }    
  16.  

Happy learning.

Ebook Download
View all
Learn
View all