Introduction
C# 4.0 introduced two new concepts, Named parameters and Optional parameters. In this article and code examples, let's see how we can use and take advantages of named parameters and optional parameters in C# and .NET.
Named Parameter
Named Parameters allow developers to pass a method arguments with parameter names. Prior to these this feature, the method parameters were passed using a sequence only. Now, using named parameters in C#, we can put any parameter in any sequence as long as the name is there. The right parameter value based on their names will be mapped to the right variable. The parameters name must match with the method definition parameter names.
The syntax of parameter names is (parametername: value).
In the following code example, the method AddNumber(int firstNumber, int secondNumber) takes two parameters and returns a sum of the two numbers. Here, firstNumber and secondNumber are two parameters that are passed in AddNumber() method using named parameters. If it doesn’t match the method definition parameter name and Named Parameter Name then it throws compilation error “The best overload for 'AddNumber' does not have a parameter named 'secondNumber1'”.
The following method definition I will also use in my following examples.
public static int AddNumber(int firstNumber, int secondNumber)
{
return firstNumber+ secondNumber;
}
To call the above method, we use the following method call. As you can see from this below code snippet, the name of the parameter is used with a ":" followed by the value.
AddNumber(firstNumber: 123, secondNumber: 64);
Here are some cases that show how to pass parameters in a method.
Case 1: Using Normal Way
This is the way we usually call methods. In this case, the value of the parameters are passed to the arguments in same order. In this case, the order of the parameters in the call must be exactly same as in the method definition. It means in above
AddNumber(12, 13);
example, firstNumber value will be 12 and secondNumber value will be 13.
Case 2: Using Named Parameters
AddNumber(firstNumber: 12, secondNumber: 13);
It is another way to pass parameters to a method. It is not mandatory to keep the parameter sequence because we are using named parameters. Regardless of the order of the parameters, the value of the agument will be matched based on their names. Here we are passing firstNumber as 12 and secondNumber as 13. We can write same code like:
AddNumber(firstNumber: 12, secondNumber: 13);
A parameter can be positional parameter as shown below:
AddNumber(12, secondNumber: 13);
Here we pass 12 as first parameter and 13 as second parameter.
But positional parameters doesn’t follow Named Parameters. It throws compilation error “Named argument specifications must appear after all fixed arguments have been specified”.
AddNumber(firstNumber: 12, 13); // It will throw compilation error.
Here is the complete C# code shows how to use named parameters.
static void Main(string[] args)
{
// The method can be called in the normal way, by using positional arguments.
Console.WriteLine(AddNumber(12, 13));
// Named arguments can be supplied for the parameters in either order.
Console.WriteLine(AddNumber(firstNumber: 12, secondNumber: 13));
Console.WriteLine(AddNumber(secondNumber: 13, firstNumber: 12));
// Positional arguments cannot follow named arguments. It throws compilation error.
Console.WriteLine(AddNumber(firstNumber: 12, 13);
// Named arguments can follow positional arguments.
Console.WriteLine(AddNumber(12, secondNumber: 13));
}
Optional Parameters
By default, all parameters of a method are required. But in C# 4.0, the concept of optional parameters was introduced that allows developers to declare parameters as optional. That means, if these arguments are not passed, they will be ommitted from the execution. Optional parameters are not mandatory.
For instance, there is a method called AddNumber(int firstNumber, int secondNumber) which takes two parameters and returns the sum of two parameters. By default, it is mandatory to pass both parameter values when we call the method. However Optional Parameters allow us not to pass these values.
It can be implemented in the following ways:

Saravanakumar SekaranPosted Apr 3, 2022, 6:56 AM
Wow amazing article
Shaun HunterPosted Jan 13, 2022, 11:34 AM
Thanks for the article but any chance of running your articles through a spellchecker before posting them? I count six typos in this one article (adn, parameber, Optinal, Numer, restult, thrid). Numer is a misspelt method name which isn't helpful. This really doesn't make your articles look professional and is offputting.
sandeep gatekarPosted Apr 16, 2019, 7:52 AM
Thank you very much
Ashish ZalkePosted Dec 4, 2017, 1:39 AM
Good explanation.....
Ashok SivaPosted Nov 28, 2017, 9:08 AM
Very well explanation !!!
Ramakrishna GaddePosted Jan 17, 2017, 2:22 PM
Very well explanation !!!
Debsankar PatraPosted Jun 13, 2016, 12:30 AM
well explained.
Humayun Kabir MamunPosted Nov 4, 2015, 12:31 AM
Nice...
Sibeesh VenuPosted Nov 4, 2015, 12:26 AM
Nice Share
Ajay GandhiPosted Nov 4, 2015, 12:03 AM
Cool
Ravi PatelPosted Nov 3, 2015, 11:00 AM
nice explanation
Former memberPosted Nov 3, 2015, 7:45 AM
very good article
Harshad PansuriyaPosted Nov 3, 2015, 6:14 AM
Nice one