Making Method Parameters Optional In C#

Introduction

There are 4 ways we can make method parameters optional and they are given below.

  1. Using method overloading.
  2. Using parameter arrays.
  3. Specify parameter defaults.
  4. Using OptionalAttributethat is present in System.Runtime.InteropServicesnamespace

Explanation

Using method overloading

As you can see in the  below code, I have created two functions with the the same name with the different parameters and this is called method overloading. I will get two overloaded functions of AddNumbers, where one is with three parameters – two integers and one integer array (for more than one number) and the second one has only two integers and calls the function which has 3 parameters by passing null as a third parameter. The function has 3 parameters and I am adding two integers in addition to storing it into a total variable and if the third parameter is not null, then I am looping and adding it into this total variable and display the number.

Now, you can have third parameter as an optional parameter in this method AddNumbers.

  1. staticvoid Main(string[] args) {  
  2.     AddNumbers(2, 8);  
  3.     AddNumbers(6, 5, new int[] {  
  4.         6,  
  5.         8  
  6.     });  
  7.     Read();  
  8. }  
  9. public static void AddNumbers(int a, int b) {  
  10.     AddNumbers(a, b, null);  
  11. }  
  12. publicstatic void AddNumbers(int a, int b, int[] n) {  
  13.     inttotal = a + b;  
  14.     if (n != null) {  
  15.         foreach(int i in n) {  
  16.             total += i;  
  17.         }  
  18.     }  
  19.     WriteLine(total);  
  20. }  
C#

Using Parameter array

Parameter array can be specified, using params keyword before int array.

In the function given above, AddNumbers third parameter integer array replaces this params int[] array, as shown below.

  1. staticvoid Main(string[] args) {  
  2.     AddNumbers(2, 8);  
  3.     AddNumbers(6, 5, 6, 8);  
  4.     Read();  
  5. }  
  6. public static void AddNumbers(int a, int b, params int[] n) {  
  7.     int total = a + b;  
  8.     if (n != null) {  
  9.         foreach(int i in n) {  
  10.             total += i;  
  11.         }  
  12.     }  
  13.     WriteLine(total);  
  14. }  
C#

Note

Parameter array must be the last parameter in a function parameter list, if you add this parameter array anywhere instead of last, then you will be getting compile error, as shown below.

C#

Specifying method parameter defaults

In the function AddNumbers third parameter integer array, we can make it optional by assigning null to it, as shown below.

  1. staticvoid Main(string[] args) {  
  2.     AddNumbers(2, 8);  
  3.     AddNumbers(6, 5, new int[] {  
  4.         6,  
  5.         8  
  6.     });  
  7.     Read();  
  8. }  
  9. public static void AddNumbers(int a, int b, int[] n = null) {  
  10.     int total = a + b;  
  11.     if (n != null) {  
  12.         foreach(int i in n) {  
  13.             total += i;  
  14.         }  
  15.     }  
  16.     WriteLine(total);  
  17. }  
C#

Note

An optional parameter must be the last parameter in the function parameter list.

Named parameters

By using an optional parameter, we can pass the parameter according to the name, as given below.

  1. staticvoid Main(string[] args) {  
  2.     DisplayNumbers(5);  
  3.     DisplayNumbers(2, 4);  
  4.     DisplayNumbers(1, 2, 3);  
  5.     DisplayNumbers(1, c: 2, b: 3);  
  6.     ReadKey();  
  7. }  
  8. publicstatic void DisplayNumbers(int a, int b = 10, int c = 20) {  
  9.     WriteLine("a = " + a);  
  10.     WriteLine("a = " + b);  
  11.     WriteLine("a = " + c);  
  12.     WriteLine("-------------");  
  13. }  
C# 

In the example given above, you can see I have marked in function DisplayNumber, “b” and “c” parameter as optional (here by default a “b” takes value 10 and “c” takes value 20). In main function I have called four times with passing parameter in a different way, out of this four function calls the fourth function call i.e, DisplayNumber(1,c:2,b:3). I am passing a parameter with the specific names for the optional parameters by using the name of the parameter colon and the value(example, c:2).

Using [OptionalAttribute] or [Optional]

You can make function parameter optional, using [OptionalAttribute] or [Optional] in front of method parameter by adding namespace, using System.wmRuntime.InteropServices; as shown below.

  1. staticvoid Main(string[] args) {  
  2.     AddNumbers(2, 8);  
  3.     AddNumbers(6, 5, new int[] {  
  4.         6,  
  5.         8  
  6.     });  
  7.     Read();  
  8. }  
  9. public static void AddNumbers(int a, int b, [Optional] int[] n) {  
  10.     int total = a + b;  
  11.     if (n != null) {  
  12.         foreach(int i in n) {  
  13.             total += i;  
  14.         }  
  15.     }  
  16.     WriteLine(total);  
  17. }  
C#

Note

An optional parameter must be the last parameter in the function parameter list.

Next Recommended Readings