Monday, 23 April 2012

Optional and Named parameters in C# 4.0

The major addition to C# 4.0 are:
  • Optional and named parameters
  • Dynamic Lookup
  • Variance - Covariance and Contravariance

Optional Parameters

C# 4.0 allow 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 who 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 format 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() passed 100 to x and 300 to z by using named parameter syntax, which is parameters 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 in main string as follows:
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");
pos = Search("How do you do", "do",5); // start search from 5th position
pos = Search("How do you do", "do", occurrence:2);  // start search from 0th position(taken by default) but look for 2nd occurrence. 

No comments:

Post a Comment

What should you required to learn machine learning

  To learn machine learning, you will need to acquire a combination of technical skills and domain knowledge. Here are some of the things yo...