Make Parameters Optional In C#

Introduction

 
As the name suggests, optional parameters are not mandatory. This helps developers not to pass arguments for some of the method's parameters.
 
Nowadays this is a very common interview question. In C#, there are mainly 4 ways to make method parameter optional, which are listed below.
  1. Use Parameter arrays
  2. Default parameter
  3. Use OptionalAttribute
  4. Method Overloading

Parameter arrays

 
We can implement an optional parameter by using the parameter arrays (using the params keyword). It allows us to pass any number of parameters to the methods.
 
The main use of parameter arrays is to make our program's or method's parameter optional.
 
For example, we have a method or function in our program that will allow the user to add two or more numbers that will return the sum of the provided numbers like below.
  1. public static void Sum(int NumberOne,int NumberTwo, params int[] NNumbers)  
  2. {  
  3.    int res=NumberOne + NumberTwo;  
  4.       foreach(int no in NNumbers)  
  5.       {  
  6.          res+=no;  
  7.       }  
  8.       Console.WriteLine("Sum is : {0}",res);  
  9. }  
In the above example, we can see that NumberOne and NumberTwo are compulsory and the last parameter NNumbers are optional. Parameter arrays must be the last parameter in the parameter list, meaning we can't specify parameter array in starting or between the method signature, like:
  1. public static void Sum(int NumberOne, params int[] NNumbers,int NumberTwo)  
  2. {  
  3.    // Method body  
  4. }  
The above signature will not work because as per the rules, params array must be the last in the parameter list.we can create only one params array means we can't create more than one parameter array.
 
If anyone wants to add just two numbers, they can invoke the method such as:
  1. Sum(5,25);  
Output
 
30
 
On the other side if we want to add 10 numbers then we can simply invoke the method:
  1. Sum(1,2,3,4,5,6,7,8,9,10);  
Output
 
55
 
We can also pass an object as a parameter like:
  1. Sum(1,2,new Object{3,4,5,6,7,8});  
Output
 
36
 

Default parameter values

 
We can implement optional parameters by assigning a default value for the parameters. This is the easiest and simple way to make the methods parameter optional. In this way, we just need to define the optional parameter with its default values when we create our methods. Keep in mind that when we pass default values for that optional parameter then the method will take the passed value but not their default value. The method will only accept default value when we do not pass any values for the optional parameter at the time of invoking methods.
 
Example
  1. public static void Sum(int a,int b , int[] n)  
  2. {  
  3.    int res= a+b;  
  4.    foreach(int no in n)  
  5.    {  
  6.       res+=no;  
  7.    }  
  8.    Console.WriteLine("Sum is : {0}",res);  
  9. }  
Now based on the above implementation, if we want to 5 numbers, then we have to invoke methods like:
  1. Sum(1,2,new int[] {3,4,5});  
So now, as per the above implementation, all the parameters are compulsory so as per our requirement we just want to add 2 parameters then this method will not work as
expected. If we want to use these methods then we need to pass a 3rd parameter also, so we have to pass an empty array as the 3rd parameter, like:
  1. Sum(1,2,new int[]{});  
We can make a parameter optional by assigning default values for that parameter, like:
  1. public static void Sum(int a,int b , int[] n=null)  
  2. {  
  3.    int res=a+b;  
  4.    if(n!=null)  
  5.    {  
  6.       foreach(int no in n)  
  7.       {  
  8.          res+=no;  
  9.       }  
  10.    }  
  11.    Console.WriteLine("Sum is : {0}",res);  
  12. }  
  13. // Main method  
  14. static public void Main()  
  15. {  
  16. Sum(1,2);  
  17. Sum(1,2,new int[]{3,4,5,6});  
  18. }  
Output
 
3
21
 
To make a parameter optional, we first check if the value of that parameter is not null then and then we can loop through otherwise we will get null reference exception.
 
So now as per the above implementation, we just want to add just 2 numbers we can use, like:
  1. Sum(1,2);  
To specify null values we have to take care as optional parameters must appear after required parameters. If we specified a null parameter in between the methods signature, then the methods will not compile and give us an error like:
  1. public static void Sum(int a,int b=1,int c)  
  2. {  
  3.    //Method Body  
  4. }  
The above method will not compile because we making parameter b is optional but it appears before the required parameters c so we have to take about it.
 

Named Parameter

 
Here some another interesting concept. We want to invoke methods and pass parameter values as per our choice:
  1. public static void NamedParam(int a, int b=25, int c=35)  
  2. {  
  3.    Console.WriteLine("a= : {0}",a);  
  4.    Console.WriteLine("b= : {0}",b);  
  5.    Console.WriteLine("c= : {0}",c);  
  6. }  
Now invoking the above methods:
  1. NamedParam(1,2);  
Value 1 is passed for the parameter a and value 2 is passed for parameter b as the argument by default. But I want to pass 20 as the argument for c. So to achieve this, we can use the named parameter, like below.
  1. NamedParam(1,c:20);  
Notice that we have passed the name of the parameter for which value 20 is being passed.
 

Use OptionalAttribute

 
OptionalAttribute is present in System.Runtime.InteropServices namespace so to use this we have to import this we can implement optional parameters by using OptionalAttribute.Default value for the optional attribute is zero(0).
  1. using System.Runtime.InteropServices;  
  2. public static void Sum(int a,int b , int[] n)  
  3. {  
  4.    int res= a+b;  
  5.    foreach(int no in n)  
  6.    {  
  7.       res+=no;  
  8.    }  
  9.    Console.WriteLine("Sum is : {0}",res);  
  10.  }  
Now based on the above implementation, if we want to 5 numbers then we have to invoke methods:
  1. Sum(1,2,new int[] {3,4,5});  
So now, as per the above implementation, all the parameters are compulsory.we can make the 3rd parameter optional by using OptionalAttribute.
 
Make sure first we have to import System.Runtime.InteropServices namespace.
  1. public static void Sum(int a ,int b , [Optional] int[] n)  
  2.    {  
  3.       int res=a+b;  
  4.       if(n!=null)  
  5.       {  
  6.          foreach(int no in n)  
  7.          {  
  8.             res+=no;  
  9.          }  
  10.       }  
  11.       Console.WriteLine("Sum is {0}: ", res);  
  12.   }  
Now if we want to add just 2 numbers, then we can simply call the methods:
  1. Sum(1,2);  
Output
 
3
 

Method Overloading

 
We can implement optional parameters by using method overloading. In method overloading, we create the same methods with the same name but with the different
kinds of parameters.
 
For example, we have one method that will accept parameters like shown below:
  1. public static void Sum(int a,int b , int[] n)  
  2. {  
  3.    int res= a+b;  
  4.    foreach(int no in n)  
  5.    {  
  6.       res+=no;  
  7.    }  
  8.    Console.WriteLine("Sum is : {0}",res);  
  9.  }  
Now based on the above implementation if we want to 5 numbers then we have to invoke methods like
  1. Sum(1,2,new int[] {3,4,5});  
At the moment all the 3 parameters are compulsory. If I want to add just 2 numbers, then I can invoke the method as shown below.
  1. Sum(10, 20, null);  
We can make the 3rd parameter optional by overloading Sum() function as below. Just copy the signature of the existing methods and make necessary changes as shown
below.
  1. public static void Sum(int a,int b)  
  2. {  
  3.    Sum(a,b,null);  
  4. }  
Now, we have 2 overloaded versions of the Sum() function. If I just want to add 2 numbers, then I can use the overloaded version of Sum() function,
  1. Sum(1, 2);  
Output
 
3
 
If I want to add 3 or more numbers, then I can use the overloaded version of Sum() function
  1. Sum(100, 60, new int[] { 10, 90 });  
Output
 
260
 

Summary

 
In this blog, I will try my best to share knowledge about how to make parameters optional in C#. I hope this will help you!