C# | Optional Parameters

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.

Important Points:

  • You can use optional parameters in Methods, Constructors, Indexers, and Delegates.
  • Each and every optional parameter contains a default value which is the part of its definition.
  • If we do not pass any parameter to the optional arguments, then it takes its default value.
  • The default value of an optional parameter is a constant expression.
  • The optional parameters are always defined at the end of the parameter list. Or in other words, the last parameter of the method, constructor, etc. is the optional parameter.

Example:

static public void scholar(string fname, string lname, int age = 20, string branch = “Computer Science”)

In the above example, int age = 20 and string branch = “Computer Science” is the optional parameter and has its default value. Let us discuss the concept of the optional parameter with the help of an example.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// concept of optional parameters
using System;
  
class GFG {
  
    // This method contains two regular
    // parameters, i.e. fname and lname
    // And two optional parameters, i.e.
    // age and branch
    static public void scholar(string fname, 
                               string lname,
                               int age = 20,
         string branch = "Computer science")
  
    {
        Console.WriteLine("First name: {0}", fname);
        Console.WriteLine("Last name: {0}", lname);
        Console.WriteLine("Age: {0}", age);
        Console.WriteLine("Branch: {0}", branch);
    }
  
    // Main Method
    static public void Main()
    {
  
        // Calling the scholar method
        scholar("Ankita", "Saini");
        scholar("Siya", "Joshi", 30);
        scholar("Rohan", "Joshi", 37,
           "Information Technology");
    }
}

chevron_right


Output:

First name: Ankita
Last name: Saini
Age: 20
Branch: Computer science
First name: Siya
Last name: Joshi
Age: 30
Branch: Computer science
First name: Rohan
Last name: Joshi
Age: 37
Branch: Information Technology

Explanation: In the above example, the scholar method contains four parameters out of these four parameters two are regular parameters, i.e. fname and lname and two are optional parameters, i.e. age and branch. These optional parameters contain their default values. When you do not pass the value of these 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.

What will happen if we use optional parameters other than the last parameter?

It will give the compile-time error “Optional parameter cannot precede required parameters” as optional parameters are always defined at the end of the parameter list. Or in other words, the last parameter of the method, constructor, etc. is the optional parameter.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the concept
// of optional parameters
using System;
  
class GFG {
  
    // This method contains two regular
    // parameters, i.e. fname and lname
    // And one optional parameters, i.e. age
    static public void scholar(string fname,
                 int age = 20, string lname)
    {
        Console.WriteLine("First name: {0}", fname);
        Console.WriteLine("Last name: {0}", lname);
        Console.WriteLine("Age: {0}", age);
    }
  
    // Main Method
    static public void Main()
    {
  
        // Calling the scholar method
        scholar("Ankita", "Saini");
    }
}

chevron_right


Compile-time Error:

prog.cs(11,26): error CS1737: Optional parameter cannot precede required parameters



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.