C #4.0 preliminary study: optional and named Parameters

Source: Internet
Author: User
Optional and named parameters features provide great convenience in some occasions, especially in office development, you can say goodbye to system. reflection. Missing. Here is a brief introduction to optional and named parameters in C #4.0.
The C #4.0 compiler in vs2010 CTP passes debugging, and the official version may change.

1. Example Class Program
{
Static Void Main ( String [] ARGs)
{
Test t = New Test ();
T. optionalandnamedparams ( 1 );
T. optionalandnamedparams ( 2 , " A " );
T. optionalandnamedparams ( 3 , C: " B " , B: " A " );
T. optionalandnamedparams ( 4 , C: " Http://g.cn " );
}
}

Class Test
{
// A is a required parameter; B and C are optional parameters.
Public Void Optionalandnamedparams ( Int A, String B = "" , Object C = " Http://xianfen.net " )
{
Console. writeline ( " A: {0}, B: {1}, C: {2} " , A, B, C );
}
}

The running result is:

Note:
When required parameters and optional parameters are used together in the same method, the declaration of required parameters should be placed before the optional parameters.
The initial value of an optional parameter must be a constant that can be determined during the compilation period.
Optional parameter modifiers such as REF and out are not available.
If all parameters are explicitly specified (paraname: value), the order of parameters can be adjusted as needed.

2. Internal implementation
Ildasm decompile test. optionalandnamedparams,Code Segment: . Method Public Hidebysig Instance Void Optionalandnamedparams ( Int32 A,
[Opt] String B,
[Opt] Object C) Cel Managed
{
. Param [ 2 ] = ""
. Param [ 3 ] = " Http://www.xianfen.net "
// ...

Optional parameters are declared as optionalattribute. This is the syntactic sugar of C #4.0.
The code equivalent to the test. optionalandnamedparams method is: Public Void Optionalandnamedparams ( Int A,
[Optional, defaultparametervalue ( "" )] String B,
[Optional, defaultparametervalue ( " Http://www.xianfen.net " )] Object C)
{
Console. writeline ( " A: {0}, B: {1}, C: {2} " , A, B, C );
}

If you do not need to set the default value for the defaultparametervalueattribute feature, the output is:

Now we know the reason for missing in office development.
Optionalattribute and defaultparametervalueattribute are not new classes of FCL. They declare parameters of methods as optional methods and specify default values, this allows you to call optional and default parameters (such as VB and VC ++ ).

3. Call Process
In the main method, the Il code that calls T. optionalandnamedparams (1) is (release mode compilation ):
Il_0006: Ldloc.0
Il_0007: LDC. i4.1
Il_0008: Ldstr ""
Il_000d: Ldstr " Http://www.xianfen.net "
Il_0012: Callvirt Instance Void Test: optionalandnamedparams ( Int32 ,
String ,
Object )

As you can see, the compiler first obtains the default value of the optional parameter and then calls it like a conventional method. This is not a final state.ProgramWhen you update an Assembly that contains optional parameter methods in an application, the program may be abnormal due to a change in the default value.

4. Method Overloading
When the optional parameter and method are overloaded, the method of calling the method is the same. Which of the following can be called? For example:
Add a method to the test class above and define it:Public VoidOptionalandnamedparams (IntA,StringB)
{
Console. writeline ("Overload, A: {0}, B: {1}", A, B );
}

The output result is:

We can see that the method that completely matches the parameters is called first.

References:
Msdn
Named arguments, optional arguments, and default values

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.