Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / C# 4.0

C# 4.0 Crash Course: Optional and Named Parameter

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
13 Dec 2010CC (ASA 2.5)1 min read 14K   4   2
C# 4.0 Crash Course: Optional and Named Parameter

Parameters have default value. Hence, explicit values are not specified in the calling place, default value has been taken.

C#
static void Power(int v, int p = 2)
{
 Console.WriteLine("{0} Power of {1} = {2}", v, p, Math.Pow(v, p));
}

p is the optional parameter here. By default, Power does calculate square of v, if p value is provided explicitly, it does calculate v to the power of p.

C#
Power(2, 3);  // 2 ** 3 = 8
Power(4); // 4 ** 2 = 16

Actually, two parameter target attributes help to make this feature available. These are:

  • OptionalAttribute
  • DefaultParameterValueAttribute

in System.Runtime.InteropServices namespace.

The OptionalAttribute marks the parameter as optional as shown below:

C#
static void Power(int v, [Optional] p) {...} 	// p's default value is to 
					// default(int) i.e. 0

Power(5); // prints 1

The DefaultParameterValueAttribute constructor takes value parameter of type object used to initialize the default value of the target parameter.

C#
// p's default value is 2
static void Power(int v, [Optional][DefaultParameterValue(2)] p) {...} 

Points to be noted on “optional” parameter usage:

  • Optional parameter should be the last parameter like “param” usage
    ref and out parameters.

Named Parameters

Optional parameter will not be useful in real without named parameters. Yes, the name itself tells the answer. What will you do if you have more than one optional parameters and at the calling place, you want to pass actual value for less number of optional parameters. For example:

C#
static void MoreOptionalParamMethod(int a = 0, int b = 1, int c = 2)
{
 Console.WriteLine("a= {0}, b= {1}, c= {2}, ", a, b, c);
}

Now, you want to pass the value of variable b only. Named parameter will help you.

Instead of parameter order, you can explicitly specify parameter name along with the value using “:” in the calling place.

See the following calling place:

C#
MoreOptionalParamMethod(b: 5); // prints a= 0, b= 5, c= 2

There is nothing surprising in the IL code, the compiler replaces optional parameters with default values as shown below:

C#
MoreOptionalParamMethod(0, 5, 2);

The only thing to be considered is named and normal parameters can be mixed, but normal parameter should come first.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
Architect Aditi
India India
Working as Architect for Aditi, Chennai, India.

My Website: www.udooz.net

My Blog: www.udooz.net/blog

Comments and Discussions

 
GeneralMy vote of 5 Pin
Dilip Baboo14-Mar-11 8:12
Dilip Baboo14-Mar-11 8:12 
GeneralMy vote of 3 Pin
Srikanth0319-Dec-10 22:49
Srikanth0319-Dec-10 22:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.