Optional Arguments were introduced in C#
4.0 and this article will show you how to use them. Optional Arguments are necessary when you
specify functions that have a large number of arguments. It can cut down time by making
you pass on argument values to the most needy parameters in a situation.
Optional Arguments are what their name
suggests i.e. even if you do not specify them, its perfectly OK. But it doesn't
mean that they have not taken a value. In fact we specify some default values
that the Optional Parameters take when values are not supplied in the function
call. In this way we gain the functionality of easily setting values once and
using these values several times as a default unless something different is
needed.
The values that we supply in the function
Definition are some constants. Note that we can only supply one default value
for one parameter. This makes sense as there can only be one default value for
one property by definition. But this also means that
multiple values cannot be assigned and given priorities in
different situations. We have to check for ourselves which value fits in best in a
given situation and supply it in a function call to that variable. These
default values are to be used in case no value is supplied in
the function call. These values are specified by typing in equations(with a
single = sign) instead of just the declaration of variables.
For example, we write
static void Func(Str = "Default
Value")
instead of static void Func(int Str)
If you didn't know this technique, you were
probably using Function Overloading, but that technique requires multiple
declaration of the same function. Using this technique you can define the
function only once and have the functionality of multiple function
declarations.
Consider a function that is defined with two
optional parameters. When only one parameter is supplied, the second parameter takes the default value. We can then write the
function in such a way as to check if the second parameter value received or not. If the parameter value is
the default one then just ignore it and continue to work with the first
parameter value. Thus we have the function overloading effect in which a same
named function takes on two different tasks.
When we supply two different parameters, the function works with both the values. This can be easily achieved using the if statement. Just check which parameter has and does not have the default value.
When we supply two different parameters, the function works with both the values. This can be easily achieved using the if statement. Just check which parameter has and does not have the default value.
When using this technique it is best that you
have declared optional & required parameters both. i.e. you can specify
both types of parameters in your function declaration to get the most out of
this technique.
An example of a function that uses both types
of parameters is shown below:
static
void Func(String Name, int Age, String Address = "N/A")
In the example above, Name & Age are
required parameters. The string Address is optional, if not
specified, then it would take the value "N/A" meaning the Address is
not available.
For the above example, you could have two
types of function calls:
- Func("John Chow", 30, "America");
- Func("John Chow", 30);
That's it from Optional Arguments. Have a
look at Named Arguments for more information on
parameter types.