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 Optional Attribute that is present in System.Runtime.InteropServicesnamespace.

Making Parameters optional 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.

using System;

class Program
{
    static void Main(string[] args)
    {
        AddNumbers(2, 8);
        AddNumbers(6, 5, new int[] { 6, 8 });
        Read();
    }

    public static void AddNumbers(int a, int b)
    {
        AddNumbers(a, b, null);
    }

    public static void AddNumbers(int a, int b, int[] n)
    {
        int total = a + b;
        if (n != null)
        {
            foreach (int i in n)
            {
                total += i;
            }
        }
        Console.WriteLine(total);
    }

    public static void Read()
    {
        Console.ReadLine();
    }
}

C#

Making Parameters optional 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.

using System;

class Program
{
    static void Main(string[] args)
    {
        AddNumbers(2, 8);
        AddNumbers(6, 5, 6, 8);
        Read();
    }

    public static void AddNumbers(int a, int b, params int[] n)
    {
        int total = a + b;
        if (n != null)
        {
            foreach (int i in n)
            {
                total += i;
            }
        }
        Console.WriteLine(total);
    }

    public static void Read()
    {
        Console.ReadLine();
    }
}
q

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.

using System;

class Program
{
    static void Main(string[] args)
    {
        AddNumbers(2, 8);
        AddNumbers(6, 5, new int[] { 6, 8 });
        Read();
    }

    public static void AddNumbers(int a, int b, int[] n = null)
    {
        int total = a + b;
        if (n != null)
        {
            foreach (int i in n)
            {
                total += i;
            }
        }
        Console.WriteLine(total);
    }

    public static void Read()
    {
        Console.ReadLine();
    }
}

C#

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

Named parameters in C#

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

using System;

class Program
{
    static void Main(string[] args)
    {
        DisplayNumbers(5);
        DisplayNumbers(2, 4);
        DisplayNumbers(1, 2, 3);
        DisplayNumbers(1, c: 2, b: 3);
        Console.ReadKey();
    }

    public static void DisplayNumbers(int a, int b = 10, int c = 20)
    {
        Console.WriteLine("a = " + a);
        Console.WriteLine("b = " + b);
        Console.WriteLine("c = " + c);
        Console.WriteLine("-------------");
    }
}

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).

Making parameter optional 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.

using System;
using System.Runtime.InteropServices;

class Program
{
    static void Main(string[] args)
    {
        AddNumbers(2, 8);
        AddNumbers(6, 5, new int[] { 6, 8 });
        Read();
    }

    public static void AddNumbers(int a, int b, [Optional] int[] n)
    {
        int total = a + b;
        if (n != null)
        {
            foreach (int i in n)
            {
                total += i;
            }
        }
        Console.WriteLine(total);
    }

    public static void Read()
    {
        Console.ReadLine();
    }
}

C#

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


Recommended Free Ebook
Similar Articles