Introduces named and optional arguments in C# 4.0. We can use optional parameters in Methods, Constructors, Indexers, and Delegates.
Generally, while calling the method you need to pass all the method parameters. If you specify a few method parameters as optional, then you can omit those parameters while calling the method because the optional parameters are not mandatory.
If you want to make the parameter optional, you need to assign the default value to that parameter as part of its definition in methods, constructors, indexers, and delegates.
While calling the method, if no argument is sent for an optional parameter, then the default value will be used; otherwise, it will use the value sent for that parameter.
public void GetDetails(int id, string name, string address= "Kathmandu")
{
// code here
}
If we observe given above method, we made the address parameter optional by assigning the default value (Kathamandu). So, while accessing the Details() method, if no argument is sent for the address parameter, it will consider the default “Kathmandu” value; otherwise, it will use the value sent for that parameter
The defined Details() method can be accessed as given below.
GetDetail(101,"Sylvia");
GetDetail(102,"Rahul", "Kathmandu");
While defining the optional parameters, you need to make sure that the optional parameters are defined at the end of the parameter list, after any required parameters like as you defined in the given above method; otherwise, you will get the compile-time error like “optional parameters must appear after all required parameters”.
In the following example invalid way of defining the optional parameters in a method.
public void Details(int id, string address= "Kathmandu", string name)
{
// put your code here
}
The given above method will throw the compile-time error because there is defined a required parameter (name) after the optional parameter (address). As per rules, the optional parameters must define at the end of the parameter list.
C# Multiple Optional Parameters
If we want to define multiple optional parameters, then the method declaration must be as given below.
public void Details(int id, string name = "Sylvia", string address= "Kathmandu")
{
// put your code here
}
The defined Details() method can be accessed as shown below.
Details(101);
Details(102, "Sylvia");
Details(103, "Rahul", "Kathmandu");
While calling the method, if you provide an argument for any of the succession optional parameters, you must provide an argument for all the preceding optional parameters. The comma-separated gaps in the argument list are not supported.
The following is the invalid way of defining the optional parameters in a method.
Details(101, , "Sylvia");
The given above caller method will throw a compile-time error such as “Argument missing” because you provided an argument for the third parameter but not for a second.
However, if we know the third parameter's name, we can use a named argument to send a value like as given below.
Details(101, address: "Katmandu");
This is how you can skip preceding optional parameters by using named arguments in caller methods.
C# Optional Parameters Example
using System;
namespace FindAndSolve
{
public class Program {
public static void Main(string[] args)
{
Details(101);
Details(102, "Sylvia");
Details(103, "Rahul", "Kathmandu");
Details(104, address: "Pokhara");
Console.ReadLine();
}
public static void Details(int id, string name = "Sundar", string address = "Kathmandu")
{
Console.WriteLine("Id:{0}", id);
Console.WriteLine("Name:{0}", name);
Console.WriteLine("Location:{0}", address);
}
}
}
Output
Id:101
Name:Sundar
Location:Kathmandu
Id:102
Name:Sylvia
Location:Kathmandu
Id:103
Name:Rahul
Location:Kathmandu
Id:104
Name:Suresh
Location:Pokhara
This is how you can define the optional parameters in methods, indexers, constructors, and delegates based on our requirements.
C# Optional Parameters with OptionalAttribute
In c# prorammin language, you can also define the optional parameters by using the .NET OptionalAttribute class. If you define the optional parameters using OptionalAttribute class, then the optional parameters won’t expect any default values.
The OptionalAttribute class is used System.Runtime.InteropServices namespace. So, we need to import the System.Runtime.InteropServices namespace in your application to implement optional parameters.
You can define the optional parameters by mentioning the Optional keyword, which is surrounded by square brackets in front of the parameters like as given below.
public void GetDetails(int id, [Optional]string name, [Optional]string address)
{
//put your code here
}
If we observe as given above c# code, defined one required parameter (id) and two optional parameters (name, address) without specifying any default values by mentioning OptionalAttribute class Optional keyword.
C# Optional Parameters with OptionalAttribute Example
using System;
using System.Runtime.InteropServices;
namespace FindAndSolve
{
public class Program
{
public static void Main(string[] args)
{
Details(101);
Details(102, "Sylvia");
Details(103, "Rahul", "Kathmandu");
Details(104, address: "Pokhara");
Console.ReadLine();
}
public static void Details(int id, [Optional]string name, [Optional]string address)
{
Console.WriteLine("Id:{0}", id);
if (!string.IsNullOrEmpty(name))
{
Console.WriteLine("Name:{0}", name);
}
if (!string.IsNullOrEmpty(address))
{
Console.WriteLine("Address:{0}", address);
}
}
}
}
Output
Id:101
Id:102
Name:Sylvia
Id:103
Name:Rahul
Address:Kathmandu
Id:104
Address:Pokhara
C# Optional Parameters Overview
- In the C# the optional parameter introduced 4.0 to specify the parameters as optional while defining the methods, indexers, constructors, and delegates.
- To make the parameter optional, we need to assign the default value to that parameter as part of its definition in methods, indexers, constructors, and delegates.
- While calling the method, if no argument is sent for an optional parameter, then the default value will be used.
- While defining the optional parameters, we need to make sure that the optional parameters are defined at the end of the parameter list, after any required parameters.
- In c# programming language, you can also define the optional parameters using .NET OptionalAttribute class parameters without specifying any default values.