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.
// 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" ); } } |
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:
// 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" ); } } |
Compile-time Error:
prog.cs(11,26): error CS1737: Optional parameter cannot precede required parameters
Please Login to comment...