In this article, we are going to learn about the named arguments and optional parameters in C#. More importantly, we are going to see the use cases for both techniques and learn how to use them.

To download the source code for this article, you can visit our GitHub repository.

Let’s start.

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

What are Named Arguments in C#

Named Arguments in C# allow us to pass arguments to a method by associating value to the parameter name. 

The main advantage of named arguments in C# is that we can pass the arguments out of their positional order. Simply put, we don’t have to remember the order of parameters. They also increase the readability of the application, especially in cases where all the arguments are of the same type.

How to Use Named Arguments in C#

Let’s define a GetFullName method that takes the first and last name as arguments and returns the full name:

string GetFullName(string firstName, string lastName)
{
     return $"{ firstName} { lastName }";
}

When we call this method, we can pass lastName as a first argument and then the firstName argument:

var fullName = GetFullName(lastName: "Miller", firstName: "John");

Console.WriteLine(fullName); //Output: John Miller

This will still give us the expected result despite changing the position of the arguments.

However, named arguments in C# are not valid when they are followed by the unnamed arguments:

var fullName = GetFullName(lastName:"Miller", "John");

This is true only if the named argument is out of position – as it is in our example.

If the named argument is in the correct position, we can specify the unnamed arguments after the named argument:

var fullName = GetFullName(firstName:"John", "Miller");

What are Optional Parameters in C#

By default all parameters in a method are mandatory. However, optional parameters in C# allow us to omit some arguments in the method call. 

All optional parameters in C# have a default value. In case we omit the optional parameter in our method call, it will use a default value. There are two ways to achieve this in C#, using default values and Optional attribute.

Using Default Values for Optional Parameters

When we create a method, we can specify a default value for our parameters:

int Add(int num1, int num2 = 10, int num3 = 20)
{
    return num1 + num2 + num3;
}

We can now omit the second and third arguments and the method will use default values:

var addResult = Add(5);
Console.WriteLine(addResult); //Output: 35

However, there are certain rules we always have to follow when using the optional parameters.

First, we have to declare optional parameters at the end:

//This is invalid
int Add(int num1, int num2 = 10, int num3);

Also, when calling the method with the optional parameters, if we pass the value to an optional parameter, we must also pass the values to all the optional parameters preceding it:

//This is invalid
var sum = Add(10, , 100);

However, if we want to skip an optional parameter, we can use the named argument:

//This is valid
var sum = Add(10, num3: 100);

Using the Optional Attribute

Using the Optional attribute from System.Runtime.InteropServices namespace allows us to omit the arguments as well. C# will consider the default C# value for that particular type if we omit it. For int, it will be zero:

int Sum(int num1, int num2, [Optional] int num3)
{
    return num1 + num2 + num3;
}

Now, when we call the Sum method, we can omit the third argument and that parameter (num3) will have the default value of 0:

var sumResult = Sum(10, 20);
Console.WriteLine(sumResult); //Output: 30

Advantages of Named Arguments and Optional Parameters

There are several advantages of using Named arguments and Optional parameters. Named arguments are useful when we have methods with multiple optional parameters. They allow us to specify only those arguments that we need and ignore the rest. Furthermore, with named arguments, we improve the code readability and we can pass the arguments out of their positions.

Optional parameters allow us to overload our methods without having the overhead of writing the additional methods. It simplifies the method calls by allowing us to pass only those arguments that are required.

Conclusion

In this article, We have learned the uses of Named arguments and Optional parameters in C# and how to use it. Hope you enjoyed this article.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!