Named and optional parameters were two new features added in C# 4.0. Especially optional parameters have provided an alternative to creating overloaded functions to achieve a requirements. Using these two concepts is very easy. So let’s discuss them one by one.

Optional Parameters :

The basic concept of using optional parameters is to simply define extra parameters in an existing function definition, with some default values. Setting default values to the parameters, allows us to omit the need of passing any value for these new added parameters while making the function call. If no value is specified for these parameters, while calling these functions, it will use the default values which we declared in the function definition.

For ex, we have a simple function to calculate the sum of two numbers a and b. We create the instance of the class and use the sum function.

Now we would like to add another variable, say c, into the sum. But we need to use the same function and add the value of c, only if it is greater then 0. To do so, we will add the c parameter with a default value of 0, in the function definition. When we call the function, we will have two options to call this function. We can either keep calling the function, like we did above, or we can pass a value for the variable c.

When we call the function like we did above, without passing any value for c, it will always use the default value of 0 for c and use it in calculations. So results are not affected.

In second case, we can pass some value for the c, greater then 0, and it will be used in our calculations, to generate the result. So our code changes to :

This could have been done using function overloading as well, with new version of the same function. If you provide the parameter, then it calls the overloaded function or else it calls the old version of the function. The only thing here is that you need not create any overloaded version of the function and simply declare a parameter as optional. Based on its value received, you can add if conditions in your function and get the job done.

This is just a small example to use of these parameter types. Real world requirements can be more complex and we can have values of type Boolean, string or even instance of any List or class type to pass as an optional parameters.

Named Parameters :

This concept of passing parameters allows the user to explicitly specify the names of the variables, along with their values, when the function is being called. For ex, in the same example we discussed above, we can use the concept of named parameters like the following :

So simple to use these parameters. No need to worry about the order of the parameters now, as we have added the name along with the values.

But the actual advantage of named parameters comes into picture, when we use multiple optional parameters. Suppose we have 2 or more optional parameters defined in the above function, say c and d. Now when we call the function, we may either need to pass the value of c or value of d, only one at a time. So our code changes to :

But our function is having 2 variables as optional and we will be passing only one of them at a given point of time, then how it will be possible to identify which value is for c and which is for d. So this is where we can use the named parameters and explicitly specify, which parameter we are sending and which is to be used with a default value. So in our case, we will be sending a value for the variable d and using the default value of c for calculations. So we have the code as :

We can see here that d is having the value that we had set in the calling function i.e. 30 and c is using the default value of 40.

So this was about the concept of Named and Optional parameters. Hope you enjoyed reading it.