Named and Optional Parameters in C#

Named and optional parameters were two new features added to C# 4.0. Optional parameters have especially provided an alternative to creating overloaded functions to satisfy 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 necessity of passing any value for these added parameters when making the function call. If no value is specified for these parameters when calling these functions, it will use the default values that we declared in the function definition.
 
For example, 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.
 
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 than 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 continue to call the function as we did above, or we can pass a value for the variable c.
 
When we call the function as we did above, without passing any value for c then it will always use the default value of 0 for c and use it in calculations. So the results are not affected.
 
In the second case, we can pass a value for the c, greater than 0, and it will be used in our calculations, to generate the result. So our code changes to :
 
result
 
This could have been done using function overloading as well, with a 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 an overloaded version of the function and simply declare a parameter as optional. Based on its value received, you can add if conditions to your function and get the job done.
 
This is just a small example of the use of these parameter types. Actual requirements can be more complex and we can have values of type Boolean, string, or even an instance of any List or class type to pass as 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 example, in the same example we discussed above, we can use the concept of named parameters as in the following:
 
Named Parameters
 
It is very 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 is relevant when we use multiple optional parameters. Suppose we have 2 or more optional parameters defined in the preceding function, say c and d. Now when we call the function, we may either need to pass the value of c or the value of d, only one at a time. So our code changes to:
 
code
 
But our function has 2 optional variables and we will be passing only one of them at a given point in 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:
 
calculations
 
We can see here that d has the value that we set in the calling function, 30, and c uses the default value of 40.
 
So these were Named and Optional parameters. I hope you enjoyed reading it.


Similar Articles