Method Parameters declaration
We have a good with future in C# 4.0 in terms of declaring parameters with default values, so that the consuming code may or may not consume them and also this gives the option that the initializing code may or may not require a value for a parameter. This was not the case with C# until version 4.0.
Environment
VSTS 2010, Windows Server 2003
References
System.Configuration;
Namespaces Used
using System;
Where exactly we can have default parameters defined
We can use default values for the parameters for
- Constructor methods.
- Delegates.
- Methods.
Constructor with Default parameters
Create a class with constructor explicitly declared and assign value to the parameter in the constructor itself. Create an instance for the class and see that the default parameter looks optional. See below.
While compiling the code, the compiler decides to assign the default value based on whether the value is passed or not.
Now see when value is passed during initialization
Delegate with default parameters
Now let us see how to define the optional parameters for delegates. The following are two examples.
Optional Parameters
Let us see how we declare the optional parameters with default values for methods. As shown below, we can assign default parameter values in the method signature, so that the calling code needs not pass values to the parameters.
It is not mandatory that we have default values for all the parameters. See the code below.
So when we call the method we need to pass values only for the parameters that are not defaulted.
Also remember that in the parameter list, all the parameters that are not default should appear first in the list. See the code below, when it is not how VSTS behaves.
Now let us see how to override the default values. See the code below
Params keyword and default values
Now let us add a new parameter called myExperiance, experience can have more than one value. So add with a params key so that it can accept many values.
At first add with default values.
So the default value should be compile time value, which are known at compile time. Now let us see another example
So for a default value to the parameter it should always compile time constant like
- "", not like String.Empty.
- Default(DateTime), not like New DateTime(2004,4,7)
- It can be like string myConfigValue = ConfigurationManager.AppSettings[0].ToString()
Now to our myExperiance variable assign "" and see what happens.
So params variable should be the last parameter, should not have default values.
Let say we have an Enum as a parameter then we can assign default value to the parameter. Because it is primitive type and at compile time the values are known.
Let us say that we don't have default value to assign but need to set to the primitive type arbitrary value then use the default (primitive type) or new keyword.
Let us see for the anonymous methods. Anonymous methods can't have optional parameters
Named parameters
Using named parameters we can use the name of the parameters to interchange the order of the parameters while calling method.