Open In App

Different ways to make Method Parameter Optional in C#

Improve
Improve
Like Article
Like
Save
Share
Report

As the name suggests optional parameters are not compulsory parameters, they are optional. It helps to exclude arguments for some parameters. Or we can say in optional parameters, it is not necessary to pass all the parameters in the method. This concept is introduced in C# 4.0. Here we discuss the different ways to implement the optional parameter. In C#, there are 4 different types of implementation of optional parameters are available as follows:

  1. By using default value: You can implement optional parameters by using default value. It is the simplest and easiest way to implement the optional parameter. In this way, you just simply define the optional parameters with their default value in the method definition. And always remember the optional parameter is the last parameter in the parameter list of the method. In default value method, when you do not pass the value of the optional parameters, then the optional parameters use their default value and when you pass the parameters for optional parameters, then they will take the passed value not their default value.

    Example: Here, we have two regular parameters, i.e. str1 and str2 and one optional parameter, i.e. str3 with their default value. Here, the optional parameter uses this default value when we do not pass any value to the optional parameter and when we pass the value of the optional parameter in the method call, then it will take the passed value.




    // C# program to illustrate how to create
    // optional parameters using default value
    using System;
      
    class GFG {
      
        // Method containing optional parameter
        // Here str3 is a optional parameter
        // with its default value
        static public void my_add(string str1, string str2,
                             string str3 = "GeeksforGeeks")
        {
            Console.WriteLine(str1 + str2 + str3);
        }
      
        // Main method
        static public void Main()
        {
            my_add("Welcome", "to");
            my_add("This", "is", "C# Tutorial");
        }
    }

    
    

    Output:

    WelcometoGeeksforGeeks
    ThisisC# Tutorial
    
  2. By using Method Overloading: You can implement optional parameters concept by using method overloading. In method overloading, we create methods with the same name but with the different parameter list. This method is not the pure way to implement an optional parameter, but you can achieve the optional parameter concept by using this method.

    Example: Here, we have two methods with the same name but the parameter list of these methods is different, i.e. first my_mul method takes only one argument whereas second mu_mul method takes three arguments.




    // C# program to illustrate how to create
    // optional parameters using method overloading
    using System;
      
    class GFG {
      
        // Creating optional parameters
        // Using method overloading
        // Here both methods have the same 
        // name but different parameter list
        static public void my_mul(int a)
        {
            Console.WriteLine(a * a);
        }
      
        static public void my_mul(int a, 
                           int b, int c)
        {
            Console.WriteLine(a * b * c);
        }
      
        // Main method
        static public void Main()
        {
            my_mul(4);
            my_mul(5, 6, 100);
        }
    }

    
    

    Output:

    16
    3000
    
  3. By using OptionalAttribute: You can implement optional parameters by using OptionalAttribute. To implement the optional parameter first you need to add System.Runtime.InteropServices namespace in your program, then creates an optional parameter using the Optional keyword enclosed in square brackets before the definition of the parameter in the method. The default value of OptionalAttribut is zero.

    Example: Here, we create an optional parameter using OptionalAttribute, here num2 is an optional parameter in the my_mul method.




    // C# program to illustrate how to create
    // optional parameters using OptionalAttribute
    using System;
    using System.Runtime.InteropServices;
      
    class GFG {
      
        // Method containing optional parameter
        // Using OptionalAttribute
        static public void my_mul(int num,
                    [ Optional ] int num2)
        {
            Console.WriteLine(num * num2);
        }
      
        // Main Method
        static public void Main()
        {
            my_mul(4);
            my_mul(2, 10);
        }
    }

    
    

    Output:

    0
    20
    
  4. By Params Keyword: You can implement optional parameters by using the params keyword. It allows you to pass any variable number of parameters to a method. But you can use the params keyword for only one parameter and that parameter is the last parameter of the method. This method is not the pure way to implement an optional parameter, but you can achieve the optional parameter concept by using this method.

    Example: Here, optional parameter is created using the params keyword. Here a1 is the optional parameter and in which you can pass from zero to up to any number of variables.




    // C# program to illustrate how to create
    // optional parameters using params keyword
    using System;
      
    class GFG {
      
        // Method containing optional 
        // parameter Using params keyword
        static public void my_mul(int a, params int[] a1)
        {
            int mul = 1;
            foreach(int num in a1)
            {
                mul *= num;
            }
            Console.WriteLine(mul * a);
        }
      
        // Main method
        static public void Main()
        {
            my_mul(1);
            my_mul(2, 4);
            my_mul(3, 3, 100);
        }
    }

    
    

    Output:

    1
    8
    900
    


Last Updated : 29 Mar, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads