Friday, September 23, 2011

Named and Optional Arguments for Methods in C# 4.0

With C# 4.0, Microsoft has introduced a nice concept of this Named and Optional arguments for methods. So today I am going to write about how great this concept is.

I will start by writing down a very simple method. This method GetSum() will return the sum of two supplied values.

static void Main(string[] args)
{
     int sum = GetSum(5, 10); //5 and 10 are arguments
     Console.WriteLine(sum);
     Console.ReadLine();
}

static int GetSum(int i, int j) //i and j are parameters
{
     return i + j;
}

Please note the difference between arguments and parameters. Arguments are actual value you pass to the method when calling the method. Parameters are variables in the method declaration.

Named arguments enable you to specify an argument for a particular parameter by associating the argument with the parameter's name rather than with the parameter's position in the parameter list. Optional arguments enable you to omit arguments for some parameters. Both techniques can be used with methods, indexers, constructors, and delegates.

The general thing is when we are passing values to a method, they should be in the order same as in the parameter list. If I take above example, the values 5 and 10 are for i and j respectively. But when you use named and optional arguments, the arguments are evaluated in the order in which they appear in the argument list, not in the parameter list.

Named Arguments

Let's say in your method you are having 3 parameters. First one is int, second one is string and the third is again int. So when I am calling the method I will have to pass an integer first,string for next and again integer for the third. For that I should keep the order of parameter list in mind and of course it is a trouble. So with Named arguments, it will free you from the need of remembering or looking up the order of parameters in the parameter lists of called methods. The parameter for each argument can be specified by parameter name. I will modify the above example with Named arguments.

static void Main(string[] args)
{
     int sum = 0;

     //without using named arguments
     sum = GetSum(5, 10);

     //with named arguments
     //named arguments can be supplied for the parameters in either order
     sum = GetSum(i: 5, j: 10);
     sum = GetSum(j: 5, i: 10);

     //named arguments can follow positional arguments
     sum = GetSum(5, j: 10);

     //named argument 'i' specifies a parameter for which a positional argument has already been given
     //so the following statement causes a compiler error
     num = GetSum(5, i: 10);

     //positional arguments cannot follow named arguments
     //so the following statement causes a compiler error
     sum = GetSum(i: 5, 10);
}

static int GetSum(int i, int j)
{
     return i + j;
}


Optional Arguments

The definition of a method, constructor, indexer, or delegate can specify that its parameters are required or that they are optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters.

Each optional parameter has a default value as part of its definition. If no argument is sent for that parameter, the default value is used.

When defining optional parameters you should keep this in your mind. Optional parameters are defined at the end of the parameter list, after any required parameters.

I will modify the first example for you to get a good understanding about this concept.

static void Main(string[] args)
{
     int sum = 0;

     sum = GetSum(5); //sum is 30
     sum = GetSum(5,20,30); //sum is 55
     sum = GetSum(5, 20); //required_i=5, optional_j=20, optional_k=15 and sum is 40

     //the following call causes a compiler error
     //because an argument is provided for the third parameter but not for the second
     sum = GetSum(3, ,4);

     //however, if you know the name of the third parameter, you can use a named argument to give a value for third
     sum = GetSum(5, optional_k: 20); //required_i=5, optional_j=10, optional_k=20 and sum is 35

     //the following call causes a compiler error
     //because the required marametr is missing
     sum = GetSum(optional_j: 20, optional_k: 30);
}

static int GetSum(int required_i, int optional_j = 10, int optional_k = 15)
{
     return required_i + optional_j + optional_k;
}

when writing your code in Visual Studio, you will notice that IntelliSense uses brackets to indicate optional parameters, as shown in the following image.

IntelliSense


Named and optional arguments, along with support for dynamic objects and other enhancements, greatly improve interoperability with COM APIs, such as Office Automation APIs. For an example, the AutoFormat method in the Microsoft Office Excel Range interface has seven parameters, all of which are optional. So it's up to you to provide optional values, if you want to change the default values.

Feel free to give me you feedback.

Happy Coding.

Regards,
Jaliya

4 comments:

  1. Great Concept... introduced by MS and well explained by you...

    All the best.

    ReplyDelete
  2. Thanks guys.

    Happy Coding.

    Regards,
    Jaliya

    ReplyDelete
  3. Nice article and good examples given..Thanks.Also I read some useful stuff at this:
    http://dotnetfunda360.blogspot.in/2012/05/named-and-optional-arguments-in-c.html

    ReplyDelete