Tuesday, 6 September 2011

Optional Parameters and Named Arguments in C# 4.0


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.
When you use named and optional arguments, the arguments are evaluated in the order in which they appear in the argument list, not the parameter list.

Named Arguments:
Named arguments free you from the need to remember or to look up the order of parameters in the parameter lists of called methods. The parameter for each argument can be specified by parameter name. For example, a function that calculates body mass index (BMI) can be called in the standard way by sending arguments for weight and height by position, in the order defined by the function.
CalculateBMI(123, 64);
If you do not remember the order of the parameters but you do know their names, you can send the arguments in either order, weight first or height first.
CalculateBMI(weight: 123, height: 64);
CalculateBMI(height: 64, weight: 123);
Named arguments also improve the readability of your code by identifying what each argument represents.
A named argument can follow positional arguments, as shown here.
CalculateBMI(123, height: 64);
However, a positional argument cannot follow a named argument. The following statement causes a compiler error.
//CalculateBMI(weight: 123, 64);

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. A default value must be one of the following types of expressions:
·         A constant expression;
·         An expression of the form new ValType(), where ValType is a value type, such as an enum or struct;
·         An expression of the form default (ValType), where ValType is a value type.
Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list are not supported. For example, in the following code, instance method ExampleMethod is defined with one required and two optional parameters.
public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10)

The following call to ExampleMethod causes a compiler error, because an argument is provided for the third parameter but not for the second.
//anExample.ExampleMethod(3, ,4);
However, if you know the name of the third parameter, you can use a named argument to accomplish the task.
anExample.ExampleMethod(3, optionalint: 4);
IntelliSense uses brackets to indicate optional parameters.

No comments:

Post a Comment