C #4.0 new features (2) named parameters, optional parameters for interoperation with COM

Source: Internet
Author: User

C #4.0 new features (2) named parameters, optional parameters for interoperation with COM

1. Introduction

C # (2.0) is a pure object-oriented language like Java. They all use overload rather than optional parameters. However, for other external programs used, COM components often do not require all parameters to be specified (this is common in many components written by VC and VB or ironpython objects, they always use optional parameters ). This will cause a C # programmer to have to fill the entire parameter list with type. Missing. However, C #4.0 supports naming and optional parameters. The programmer can specify optional parameters through the named parameters when calling a method. All of this is to make the. NET 4.0 Dynamic Language Runtime Library (DLR) Better compatible with dynamic binding.

2. Name and optional parameters

This is another new feature for C #, but it is a natural feature for C ++, VB, and Python etc. programmers. The optional parameters in C #4.0 are roughly the same as those in other languages. Here, no additional keyword modification is required in VB, and it cannot be ignored as in C ++, it is similar to Python, and the following statement is legal:

Optional parameter foo

Static void Foo (int A, string S = "I'm a string", dynamic B = NULL, myclass c = NULL)

To put it simply, the following principles must be observed when using optional parameters in C #4.0:

0). The optional parameter must have a compile time as its default value. If it is a reference type other than string (including the special dynamic type), the default value can only be null. The following statement is:NoCompiled:

Code

Static void Foo (int A, string S = "I'm a string ", Dynamic B = 2, myclass c = new Myclass ())

1). Optional parameters must appear from right to left in the parameter list (must appear later), and parameters on the right of the optional parameters (if any) must be optional. The following statement is:NoCompiled:

Code

Static void Foo ( String s = "I'm a string" ,Int A, dynamic B = NULL, myclass c = NULL)

2). The value of an optional parameter must be specified by naming the parameter, that is, the parameter name of the optional parameter must be assigned a value. Instead of optional parameters, you do not need to use named parameters. The following calls are valid:

Foo CILS

Foo (2 );
Foo (A: 2 );
Foo (2, s: "hello", B: 3.14 );
Foo (2, s: "hello", B: 3.14, C: New myclass ());
Foo (2, C: New myclass ());

In particular, once a named parameter is used to call a method, the location of the named parameter can be in any order. The following calls are legal:

Code

Foo (B: 3.14, C: New myclass (), A: 2 );

The only effect is that B is evaluated first in the above call, followed by C and. The function always performs the evaluate operation according to the order in which the parameter appears.

In addition, optional parameters are not only applicable to common methods, but also constructor and indexer. They are essentially no different.

3. Optional parameters and heavy load Decision-Making

There is no doubt that the naming parameters and optional parameters make the CLR overload resolution in the method a little more complicated, so don't worry, here you only need to figure out the following features of heavy load decision-making:

0 ).In the method signature with optional parameters, Heavy load Decision-MakingNoThe reload version replaced by the optional parameter is accepted, for example, the following two declarations:

Public static void Foo (int A, string S = "I'm a string", dynamic B = NULL, myclass c = NULL );
Public static void Foo (int A, string S = "I'm a string ");

If you call it as follows, the compiler will prompt you that it has been confused by the above two methods:

Foo (2 );
Foo (A: 2 );

1). If the call method is the same as that of the method, the overloaded version with no optional parameters will be preferentially selected. For example, the following two methods:

Public static void Foo (int A, string S = "I'm a string", dynamic B = NULL, myclass c = NULL );
Public static void Foo (int );

If you use the following method: The called version is void Foo (int:

Foo (2 );
Foo (A: 2 );

2). When the call method is the same as that of the method, the overload decision-making will give priority to the overload with the most matching type (the easiest to convert). For example, the following two methods:

Public static void Foo (byte A, string S = "I'm a string", dynamic B = NULL, myclass c = NULL );
Public static void Foo (Object );

Both Foo (2) and foo (A: 2) Call the previous method, because int to byte is the conversion between value types, the cost is lower than that of converting from int to object.

4. com interoperability in C #4.0

The dynamic type binding mentioned in the previous article and the naming parameters mentioned earlier in this article, optional parameters provide convenience for CLR com interoperability. With the dynamic type, you do not have to explicitly convert the returned COM object when accessing the COM object because the returned is a dynamic object, we can directly call methods, attributes, and indexers on it, such as excelapp. cells [row, column]. value = "some value"; the cells [row, column] Here returns a dynamic-type object. Previously, all returned objects must be explicitly converted before they can be operated, for example: (Excel. range) excelapp. cells [row, column]). value = "some value ";

With the optional parameters, programmers do not have to use type. missing fills the entire parameter list, for example, office14 excel. applicatioinclass defines an intersect method using optional parameters:

Code

Public Virtual Range intersect (range arg1, range arg2, object arg3 = type. missing, object arg4 = type. missing, object arg5 = type. missing, object arg6 = type. missing, object arg7 = type. missing, object Arg8 = type. missing, object arg9 = type. missing, object arg10 = type. missing, object arg11 = type. missing, object arg12 = type. missing, object arg13 = type. missing, object arg14 = type. missing, object arg15 = type. missing, object arg16 = type. missing, object arg17 = type. missing, object arg18 = type. missing, object arg19 = type. missing, object arg20 = type. missing, object arg21 = type. missing, object arg22 = type. missing, object arg23 = type. missing, object arg24 = type. missing, object arg25 = type. missing, object arg26 = type. missing, object arg27 = type. missing, object arg28 = type. missing, object arg29 = type. missing, object arg30 = type. missing );

In the past, it was really crazy to manually specify these thirty parameters.

In terms of performance, since the four C # compilers can partially compile PIAs into your program set on demand, it is not necessary to fully load this large set of interoperability assembly every time, this is helpful for improving the interaction between COM and Alibaba Cloud. In addition, the syntax sugar means that parameters can be passed without ref when calling COM, which avoids programmers from creating temporary variables to hold these parameters, however, if you do not use ref here, it does not mean that you do not pass by reference but by value. In fact, the C # compiler will help you create temporary variables and pass parameters to the caller by reference.

5. Summary

A large part of the features in C #4.0 make up for some of the previously unpleasant aspects of developers, whether it is dynamic or optional parameters, new C # Let those and various components (COM, ironpython etc .) programmers who are dealing with them are relieved to some extent. C # is becoming more and more people-oriented, more accurate, and programmer-oriented.

 

Author: freesc Huang @ cnblogs

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.