C# 4.0 Method Parameters

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
  1. Constructor methods.
  2. Delegates. 
  3. Methods.
Constructor with Default parameters
 
Create a class with the 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.
 
1.gif
 
While compiling the code, the compiler decides to assign the default value based on whether the value is passed or not.
 
2.gif
 
Now see when the value is passed during initialization
 
3.gif
 
Delegate with default parameters
 
Now let us see how to define the optional parameters for delegates. The following are two examples. 
 
4.gif
 
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 to pass values to the parameters.
 
5.gif
 
It is not mandatory that we have default values for all the parameters. See the code below.
 
6.gif
 
So when we call the method we need to pass values only for the parameters that do not default.
 
7.gif
 
Also remember that in the parameter list, all the parameters that do not default should appear first in the list. See the code below, when it is not how VSTS behaves.
 
8.gif
 
Now let us see how to override the default values. See the code below
 
9.gif
 
Params keyword and default values
 
Now let us add a new parameter called myExperiance, experience can have more than one value. To add with params key so that it can accept many values.
At first add with default values.
 
10.gif
 
So the default value should be compile-time value, which is known at compile time. Now let us see another example
 
11.gif
 
So for a default value to the parameter, it should always compile-time constant like 
  1. "", not like String.Empty.
  2. Default(DateTime), not like New DateTime(2004,4,7)
  3. It can be like string myConfigValue = ConfigurationManager.AppSettings[0].ToString()
Now to our myExperiance variable assign "" and see what happens.
 
12.gif
 
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 a default value to the parameter. Because it is a primitive type and at compile time the values are known.
 
13.gif
 
Let us say that we don't have a default value to assign but need to set it to the primitive type arbitrary value then use the default (primitive type) or new keyword.
 
14.gif
 
Let us see for the anonymous methods. Anonymous methods can't have optional parameters 
 
15.gif
 
Named parameters
 
Using named parameters we can use the name of the parameters to interchange the order of the parameters while calling the method.
 
16.gif


Similar Articles