Srikanth Technologies

Optional and Named parameters in C# 4.0

The latest version of C# (C# 4.0) provides a couple of new features. In this blog I write about Optional and Named parameters.

C# 4.0 and ASP.NET 4.0 are part of .NET 4.0. Visual Studio.NET 2010 supports .Net 4.0 and it is in its beta 1 as of now.  In order to try new features of C# 4.0, you need to install VS2010, which can be downloaded from Visual Studio Website.

The major addition to C# 4.0 are:

Optional Parameters

C# 4.0 allows us to have optional parameters for methods. It is possible to declare some parameters as optional by providing default values for them. The following example shows how to create optional parameters.
static void Fun(int x, int y = 20, int z = 30)
{
    Console.WriteLine("{0},{1},{2}", x, y, z);
}
We can call function Fun() by passing either one, two or three parameters as the following code shows:
Fun(10);        // y and z default to 20 and 30
Fun(1, 2, 3);   //  passing all three parameters
Fun(100, 200);  // x = 100, y = 200 and z defaults to 30
The rule to be remembered is default argument must be on the right most. So the following is INVALID.
static void Fun(int x = 10, int y, int z)  // cannot have mandatory parameters after optional parameter
{
    // code
}
You cannot have mandatory parameter on the right of optional parameters. However, it is valid to make all parameters default if you want.

Named parameters

When we call a function, we need to pass parameters by position. I mean first actual parameter goes to first formal parameter and so on. In C# 4.0, named parameters were introduced. We can pass values to parameters by referring to parameters by names. The following example shows how to call function Fun() by using named parameters.
Fun(100, z: 300);  // 100 is passed to x and 300 to z
The above call to function Fun() passes 100 to x and 300 to z by using named parameter syntax, which is parameter name followed by : then value to be passed. Here is the syntax:
Parametername : value
Through generally named parameters are used with optional parameters they can also be used with mandatory parameters as shown below:
static void Print(int x, int y)
{        
   Console.WriteLine("Values are {0}, {1}", x, y);
}
The above function can be invoked as shown below. It relieves you from knowing the position of parameters. Of course you need to know the name of the parameters in this case.
Print(y: 10, x: 20);        
Here is another example that uses optional and named parameters - a function to search for occurrence of a substring.
static void Search(string sentence, string word, int spos = 0, int occurrence = 1)
{
      // code
}
The above function takes two mandatory parameters and two optional parameters. You can call the above function using any of the following ways.
 pos = Search("How do you do", "do");
 // start search from 5th position
 pos = Search("How do you do", "do",5); 
 // start search from 0th position(taken by default) but look for 2nd occurrence. 
 pos = Search("How do you do", "do", occurrence:2); 
I will post more blogs on new features of C# 4.0 and ASP.NET 4.0 in the upcoming blogs.

Srikanth