Optional parameters are parameters that are not compulsory to pass when calling a method. They have a default value, which is used automatically when no value is provided. They provide flexibility by allowing a method to be called with fewer arguments when needed.
- They allow us to omit arguments for certain parameters while calling a method.
- Optional parameters are always declared after the required parameters.
Example: Program to demonstrates optional parameters in C# with default values.
using System;
class GFG
{
// fname and lname are required parameters
// age and branch are optional parameters
static void Scholar(
string fname,
string lname,
int age = 20,
string branch = "Computer Science")
{
Console.WriteLine("First name: " + fname);
Console.WriteLine("Last name: " + lname);
Console.WriteLine("Age: " + age);
Console.WriteLine("Branch: " + branch);
}
static void Main()
{
// Calling the method without optional parameters
Scholar("Ankita", "Saini");
// Providing a value for age
Scholar("Siya", "Joshi", 30);
// Providing values for both optional parameters
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 T...
Explanation: The Scholar() method has two required parameters (fname, lname) and two optional parameters (age, branch) with default values. If optional values are not provided, C# uses their default values; if values are provided, they replace the defaults.
What happens if we use an optional parameter before a required parameter?
An optional parameter cannot come before a required parameter. Required parameters must always be declared before optional parameters, otherwise C# gives a compile-time error.
Example: Program to shows required parameters declared before optional parameters in a C# method.
using System;
class GFG
{
// fname and lname are required parameters
// age and branch are optional parameters
static void Scholar(
string fname,
string lname,
int age = 20,
string branch = "Computer Science")
{
Console.WriteLine("First name: " + fname);
Console.WriteLine("Last name: " + lname);
Console.WriteLine("Age: " + age);
Console.WriteLine("Branch: " + branch);
}
static void Main()
{
// Calling the method without optional parameters
Scholar("Ankita", "Saini");
// Providing a value for age
Scholar("Siya", "Joshi", 30);
// Providing values for both optional parameters
Scholar("Rohan", "Joshi", 37, "Information Technology");
}
}
Compile-time Error:
error CS1737: Optional parameter cannot precede required parameters
Correct Declaration
The required parameters should be declared first, followed by the optional parameters.
static void Scholar(
string fname,
string lname,
int age = 20)
{
Console.WriteLine("First name: " + fname);
Console.WriteLine("Last name: " + lname);
Console.WriteLine("Age: " + age);
}
Here, fname and lname are required parameters, while age is an optional parameter with a default value of 20.
Note: Optional parameters make method calls more flexible by allowing us to skip arguments when they are not required. They must have a default value and must be declared after all required parameters.