Four ways to making a method parameter optional
1. Use parameter arrays
Let's say we have a console application project in which we need to create a method that can add two or more numbers, meaning there is no limit on how many values a user will pass. For example, if a user wants to add 50 different numbers, your method should be capable of doing so and on the other hand, if the user wants to add only two numbers, your method should be capable of doing that too.
So, let's see how to implement that kind of method.
Currently my console application looks like this.
Let's a create method that will add two numbers.
We have created a static method “Add” that has two parameters, FN and SN. Both of these parameters are of type integer.
Inside this method, we have declared a new variable “sum” in which the sum of FN and SN will be stored. Then we are printing the result on the console screen.
Currently it looks like our method is only able to add two numbers and the user with that requirement is happy. But there is another user who wants to add 5 numbers and to make that user satisfied we need to make some changes in our method.
We have added a third parameter of type params int[].
This parameter can hold an array of numbers and to retrieve all the values from an array we can use a foreach loop.
Using a foreach loop we are assigning all the values from MoreNumbers in numbers and inside this loop we are adding all the numbers one by one with the current sum and in the end we are printing the number on the console window.
So, we have created our method that can add two numbers and more than two numbers.
The next step is to invoke this method in our Main method and since it is a static method we can invoke this method by just calling the name of the class like this.
1. Use parameter arrays
Let's say we have a console application project in which we need to create a method that can add two or more numbers, meaning there is no limit on how many values a user will pass. For example, if a user wants to add 50 different numbers, your method should be capable of doing so and on the other hand, if the user wants to add only two numbers, your method should be capable of doing that too.
So, let's see how to implement that kind of method.
Currently my console application looks like this.
Let's a create method that will add two numbers.
We have created a static method “Add” that has two parameters, FN and SN. Both of these parameters are of type integer.
Inside this method, we have declared a new variable “sum” in which the sum of FN and SN will be stored. Then we are printing the result on the console screen.
Currently it looks like our method is only able to add two numbers and the user with that requirement is happy. But there is another user who wants to add 5 numbers and to make that user satisfied we need to make some changes in our method.
We have added a third parameter of type params int[].
This parameter can hold an array of numbers and to retrieve all the values from an array we can use a foreach loop.
Using a foreach loop we are assigning all the values from MoreNumbers in numbers and inside this loop we are adding all the numbers one by one with the current sum and in the end we are printing the number on the console window.
So, we have created our method that can add two numbers and more than two numbers.
The next step is to invoke this method in our Main method and since it is a static method we can invoke this method by just calling the name of the class like this.
- ClassName.MethodName();
Pass each value separated by commas.
Run the application
And if the user wants to add only two numbers:
We haven't specified any values for the third parameter and even after this if you compile and run the application, it will not give us any error because of the params keyword.
Now, if we don't specify any value, the third parameter does not count since params[] are optional but what will happen if we pass null?
Run the application
We will get an exception.
So, it is always a good practice to check whether params[] is null or not and for that before looping thru the numbers we need to check if MoreNumbers is null or not.
If you run the application now, you will not get an exception.
Note
Params array must be the last parameter in a parameter list.
2. Method Overloading
To make a parameter optional, we can use method overloading. Using method overloading, we can create several methods with the same name but with a different method signature.
So, first let's create a method in which the user can pass n numbers.
The preceding code is the same as what we have seen in the params int[] demo.
In the main method, we can invoke this method directly by calling its class because this method is static and we don't need to create an instance of the class where this method is present.
Run the application
Now, what will happen if I remove the third parameter argument from the Add method?
We will get a compile time error because there is no overload that takes two arguments in this method. But what we can do is we can pass null in the third parameter.
If we run the application, we will get the sum of the two numbers.
But does this make the parameter optional?
No. Because, to make this method work we passed null for the third parameter and to overcome this problem we can create another overloaded version of this “Add” that takes two parameters.
Now, if we invoke the Add method in our main method, it will give us the suggestion of the two overloaded versions of it.
So, it now satisfies the user's requirements and based on that they will choose from the two overloaded versions of the Add method.
3. By specifying parameter defaults
The third way to make a parameter optional is by specifying parameter defaults.
Currently in my project, I have this Add method that expects three arguments.
To add n numbers, the preceding method works perfectly. But if we pass only FN and SN values it gives us the compile time error.
The reason for this error is, our Add method does not provide an overload version where we can specify values for two parameters and to make this method work we need to assign null for the third parameter that we saw in method overloading.
But there is another way to do the same thing by specifying parameter defaults. If you want to make a parameter optional, then specify a default value for that parameter.
In the preceding image, we have passed null as a default value for the third parameter.
Note
You can pass any default value.
An optional parameter must appear after all the required parameters.
In the main method, pass the arguments for the first two parameters.
Run the application
What if a user wants to add more than two numbers and he/she wants to pass those values in the third parameter? So, is it possible to pass any values for that parameter? Because currently we have specified null for it.
Yes. The user can pass the values for the third parameter.
Run the application
Named Parameters
Let's look at another example where Named parameters can be used.
In my demo I have three parameters in a static method “Add”. Out of these three parameters the last two parameters are optional since we have specified default values for it.
Invoke this method in your main method and pass value only for the first parameter.
Run the application
Now, let's pass the value for the second parameter too.
Run the application
Now what if I don't want this value of 2 for the second parameter, but I want this value for the third parameter. How is it possible to do this?
If we pass null for the second parameter, we will get an error.
If you look at the order we pass the parameters, first we pass a value for the first parameter, then the second parameter and then the third parameter.
In this kind of situation we can use named parameters.
Named parameter means name of the parameter. In our Add method, the name of the third parameter is TN. So, in order to pass the value 2 for this parameter we can say TN:2 where TN is the parameter name and 2 is the value.
Run the application
4. By using OptionalAttribute
Currently in my project, I have this Add method that expects three arguments and all these parameters are mandatory.
The preceding method is the same as what we saw in the last two ways of making a parameter optional.
To add n numbers, the preceding method works perfectly. But if we pass only FN and SN values it give us the compile time error.
The reason for this error is, our Add method does not provide an overloaded version where we can specify values for two parameters and to make this method work we need to assign null for the third parameter.
To make the third parameter null, we can use OptionalAttribute that is present in the System.Runtime.InteropServices namespace.
Make the third parameter optional.
Assign the values for the first two parameters.
Run the application
But we can also pass the values for the third parameter.
Run the application
In this article we saw four ways to make a parameter optional and we have also seen the advantage of using Named Parameters.
Thank you.
Run the application
And if the user wants to add only two numbers:
We haven't specified any values for the third parameter and even after this if you compile and run the application, it will not give us any error because of the params keyword.
Now, if we don't specify any value, the third parameter does not count since params[] are optional but what will happen if we pass null?
Run the application
We will get an exception.
So, it is always a good practice to check whether params[] is null or not and for that before looping thru the numbers we need to check if MoreNumbers is null or not.
If you run the application now, you will not get an exception.
Note
Params array must be the last parameter in a parameter list.
2. Method Overloading
To make a parameter optional, we can use method overloading. Using method overloading, we can create several methods with the same name but with a different method signature.
So, first let's create a method in which the user can pass n numbers.
The preceding code is the same as what we have seen in the params int[] demo.
In the main method, we can invoke this method directly by calling its class because this method is static and we don't need to create an instance of the class where this method is present.
Run the application
Now, what will happen if I remove the third parameter argument from the Add method?
We will get a compile time error because there is no overload that takes two arguments in this method. But what we can do is we can pass null in the third parameter.
If we run the application, we will get the sum of the two numbers.
But does this make the parameter optional?
No. Because, to make this method work we passed null for the third parameter and to overcome this problem we can create another overloaded version of this “Add” that takes two parameters.
Now, if we invoke the Add method in our main method, it will give us the suggestion of the two overloaded versions of it.
So, it now satisfies the user's requirements and based on that they will choose from the two overloaded versions of the Add method.
3. By specifying parameter defaults
The third way to make a parameter optional is by specifying parameter defaults.
Currently in my project, I have this Add method that expects three arguments.
To add n numbers, the preceding method works perfectly. But if we pass only FN and SN values it gives us the compile time error.
The reason for this error is, our Add method does not provide an overload version where we can specify values for two parameters and to make this method work we need to assign null for the third parameter that we saw in method overloading.
But there is another way to do the same thing by specifying parameter defaults. If you want to make a parameter optional, then specify a default value for that parameter.
In the preceding image, we have passed null as a default value for the third parameter.
Note
You can pass any default value.
An optional parameter must appear after all the required parameters.
In the main method, pass the arguments for the first two parameters.
Run the application
What if a user wants to add more than two numbers and he/she wants to pass those values in the third parameter? So, is it possible to pass any values for that parameter? Because currently we have specified null for it.
Yes. The user can pass the values for the third parameter.
Run the application
Named Parameters
Let's look at another example where Named parameters can be used.
In my demo I have three parameters in a static method “Add”. Out of these three parameters the last two parameters are optional since we have specified default values for it.
Invoke this method in your main method and pass value only for the first parameter.
Run the application
Now, let's pass the value for the second parameter too.
Run the application
Now what if I don't want this value of 2 for the second parameter, but I want this value for the third parameter. How is it possible to do this?
If we pass null for the second parameter, we will get an error.
If you look at the order we pass the parameters, first we pass a value for the first parameter, then the second parameter and then the third parameter.
In this kind of situation we can use named parameters.
Named parameter means name of the parameter. In our Add method, the name of the third parameter is TN. So, in order to pass the value 2 for this parameter we can say TN:2 where TN is the parameter name and 2 is the value.
Run the application
4. By using OptionalAttribute
Currently in my project, I have this Add method that expects three arguments and all these parameters are mandatory.
The preceding method is the same as what we saw in the last two ways of making a parameter optional.
To add n numbers, the preceding method works perfectly. But if we pass only FN and SN values it give us the compile time error.
The reason for this error is, our Add method does not provide an overloaded version where we can specify values for two parameters and to make this method work we need to assign null for the third parameter.
To make the third parameter null, we can use OptionalAttribute that is present in the System.Runtime.InteropServices namespace.
Make the third parameter optional.
Assign the values for the first two parameters.
Run the application
But we can also pass the values for the third parameter.
Run the application
In this article we saw four ways to make a parameter optional and we have also seen the advantage of using Named Parameters.
Thank you.
No comments:
Post a Comment