Optional Parameter in C#

In this article, I am describing how to create a method with optional parameter in C#.

Introduction

This article explains the various ways to make method parameters optional. This is a very common interview question in C# Interview Questions.

There are following 4 ways to make method parameters optional.

  1. Using parameter arrays
  2. Using method overloading
  3. Using parameter defaults
  4. Using OptionalAttribute that is present in the System.Runtime.InteropServices namespace

1. Using parameter arrays

  1. using System;  
  2.   
  3. namespace OptionalParameter  
  4. {  
  5.     class Program  
  6.     {  
  7.         public static void Sum(int firstNumber, int secondNumber, params object[] restOfTheNumbers)  
  8.         {  
  9.             int result = firstNumber + secondNumber;  
  10.             foreach (int i in restOfTheNumbers)  
  11.             {  
  12.                 result += i;  
  13.             }  
  14.   
  15.             Console.WriteLine("Total = " + result.ToString());  
  16.         }  
  17.   
  18.         public static void Main()  
  19.         {  
  20.             //Sum of two Numbers  
  21.             Sum(10, 20);  
  22.   
  23.             //Sum of more than two numbers  
  24.             Sum(10, 20, 30, 40, 50);  
  25.         }  
  26.     }  
  27. }  
The Sum method allows us to add 2 or more numbers. The firstNumber and secondNumber parameters are mandatory, whereas the MoreNumbers parameter is optional.
 
Output

 

Note:
A parameter array must be the last parameter in a formal parameter list. The following method will not compile and gives a compile time error.
  1. public static void Sum(int firstNumber, params object[] MoreNumbers,   
  2.     int secondNumber)  
  3. {  
  4.     // Function implementation  
  5. }   
2. Using method overloading
  1. using System;  
  2.   
  3. namespace OptionalParameter  
  4. {  
  5.     class Program  
  6.     {  
  7.         public static void Sum(int firstNumber, int secondNumber, int[] MoreNumbers)  
  8.         {  
  9.             int result = firstNumber + secondNumber;  
  10.             if (MoreNumbers != null)  
  11.             {  
  12.                 foreach (int i in MoreNumbers)  
  13.                 {  
  14.                     result += i;  
  15.                 }  
  16.             }  
  17.             Console.WriteLine("Sum = " + result);  
  18.         }  
  19.         public static void Sum(int firstNumber, int secondNumber)  
  20.         {  
  21.             int result = firstNumber + secondNumber;  
  22.             Console.WriteLine("Sum = " + result);  
  23.         }  
  24.         public static void Main()  
  25.         {  
  26.             //Sum of two Numbers  
  27.             Sum(10, 20);  
  28.   
  29.             //Sum of more than two numbers  
  30.             Sum(10, 20, new int[]{30,40,50});  
  31.         }  
  32.     }  
  33. }  
In the preceding example, I have two Sum methods that are overloaded or we can say there are 2 versions of the Sum method.

Output

 

3. Parameters optional by specifying parameter defaults
  1. using System;  
  2.   
  3. namespace OptionalParameter  
  4. {  
  5.     class Program  
  6.     {  
  7.         public static void Sum(int firstNumber, int secondNumber, int[] MoreNumbers=null)  
  8.         {  
  9.             int result = firstNumber + secondNumber;  
  10.             if (MoreNumbers != null)  
  11.             {  
  12.                 foreach (int i in MoreNumbers)  
  13.                 {  
  14.                     result += i;  
  15.                 }  
  16.             }  
  17.             Console.WriteLine("Sum = " + result);  
  18.         }  
  19.         public static void Main()  
  20.         {  
  21.             //Sum of two Numbers  
  22.             Sum(10, 20);  
  23.   
  24.             //Sum of more than two numbers  
  25.             Sum(10, 20, new int[]{30,40,50});  
  26.         }  
  27.     }  
  28. }  
In the preceding example, I have set MoreNumbers=null by default and when I call Sum(10,20) then by default the value passed to MoreNumber=Null and when I call the same method like Sum(10,20, new int[]{30,40,50}) then it changes the default value.

Output

 

Example 2:
  1. using System;  
  2.   
  3. namespace OptionalParameter  
  4. {  
  5.     class Program  
  6.     {  
  7.         public static void Test(int a,int b=5,int c=10)  
  8.         {  
  9.             Console.WriteLine("a = " + a);  
  10.             Console.WriteLine("b = " + b);  
  11.             Console.WriteLine("c = " + c);  
  12.         }  
  13.         public static void Main()  
  14.         {  
  15.             Test(3);  
  16.             Console.WriteLine();  
  17.             Test(2, 3);  
  18.             Console.WriteLine();  
  19.             Test(5, 4, 6);  
  20.         }  
  21.     }  
  22. }  
In the preceding example, the Test method contains the 3 parameters a, b, c but we have set b=5 and c=10 so here a parameter will be a required parameter and b and c are optional parameters.
 
 

Note: Optional parameters must appear after all the required parameters, in other words the following method will not compile.
 
Assigning a value to a specific optional parameter
 
The following sample shows how to assign a value to a specific optional parameter:

 
Output

  

4. Making method parameters optional using OptionalAttribute

In this example, we will discuss OptionalAttribute in the System.Runtime.InteropServices namespace.
  1. using System;  
  2. using System.Runtime.InteropServices;  
  3.   
  4. namespace OptionalParameter  
  5. {  
  6.     class Program  
  7.     {  
  8.         public static void Sum(int firstNumber, int secondNumber, [Optional] int[] MoreNumbers)  
  9.         {  
  10.             int result = firstNumber + secondNumber;  
  11.             if (MoreNumbers != null)  
  12.             {  
  13.                 foreach (int i in MoreNumbers)  
  14.                 {  
  15.                     result += i;  
  16.                 }  
  17.             }  
  18.             Console.WriteLine("Sum = " + result);  
  19.         }  
  20.         public static void Main()  
  21.         {  
  22.             //Sum of two Numbers  
  23.             Sum(10,20);  
  24.   
  25.             //Sum of more than two numbers  
  26.             Sum(10, 20, new int[] { 30, 40, 50 });  
  27.         }  
  28.     }  
  29. }  
In the preceding example, we have a method Sum where two parameters are required, firstNumber and secondNumber, but the last parameter MoreNumber I have marked as an [Optional] attribute.

Output

 

Thank you. Happy Coding :)