Named and
optional
parameters are really two distinct features, and allow us to either
omit parameters which have a defined default value, and/or to pass
parameters by name rather than position.
Named parameters are passed by name instead of relying on its position in the parameter list, whereas
optional parameters allow us to omit
arguments to members without having to define a specific overload matching.
Let’s have a look at Optional parameters:
http://www.codeproject.com/Articles/39014/Named-and-optional-parameters-in-C-4-0#
public static double MyOldCurrencyExchange(double amount, double rate)
{
return (amount * rate);
}
We will call the above method as shown below:
http://www.codeproject.com/Articles/39014/Named-and-optional-parameters-in-C-4-0#
MyOldCurrencyExchange(500, 1.18);
Now, by using Optional parameters:
http://www.codeproject.com/Articles/39014/Named-and-optional-parameters-in-C-4-0#
public static double MyNewCurrencyExchange(double amount, double rate=1)
{
return (amount * rate);
}
We will call the above method as shown below:
http://www.codeproject.com/Articles/39014/Named-and-optional-parameters-in-C-4-0#
MyNewCurrencyExchange (500, 1.18);
MyNewCurrencyExchange (500);
Now, by using Named parameters:
http://www.codeproject.com/Articles/39014/Named-and-optional-parameters-in-C-4-0#
MyNewCurrencyExchange (rate:1.18, amount:500);
Now, by using Named and Optional parameters:
http://www.codeproject.com/Articles/39014/Named-and-optional-parameters-in-C-4-0#
MyNewCurrencyExchange (amount:500);