C #4.0 New Feature 2: named and optional parameters (1)

Source: Internet
Author: User

This feature is relatively simple, and we will continue to talk about it for a long time, but only focus on it. In the following demo, we often use overload functions to handle problems:

Code
1 Public   Void Process ( Int P1, Float P2, String P3)
2 {
3 // Todo
4 }
5
6 Public   Void Process ( Int P1, Float P2)
7 {
8 Process (P1, P2, " Fanweixiao " );
9 }
10
11 Public   Void Process ( Int P1)
12 {
13 Process (P1, 0f );
14 }

 

In the era of C #4.0, two new functions are provided, named parameters and optional parameters ). They are two completely independent concepts, but they are often used together. The rewritten function is:

Code
1 Public   Int Nbprocess ( Int P1 = 0 , Float P2 = 0f, String P3 = " Fanweixiao " )
2 {
3 // Todo
4 }

 

In this way, we can use nbprocess (10) to call this function, which is equivalent to calling nbprocess (10, 0f, "fanweixiao.

To save the second parameter, we can call: nbprocess (10,P3: "Fanweixiao "). You can also write it as nbprocess (P1: 10, P3: "fanweixiao "). You can even change the Parameter order nbprocess (P3: "Fanweixiao ",P1: 10.

This can also be used for constructors and indexer.

For overload of such parameters, the logic is also very simple:Similarity principle. (Applicable)

From the perspective of parameter 5, M (string, INT) is excluded first, because it requires the first parameter to be of the string type. M (INT, string) is acceptable because string is an optional parameter. It and M (INT) are better than m (object), and the object is the "source of all evil, 5 and INT are much more friendly. In the end, M (INT) is the best.

The location and number of parameters of different types in the constructor are the key to determining their differentiation. This feature of C #4.0 puts the parameter name in the rule, the name of the parameter must be more serious later :). In fact, the above functions are supported in dynamic languages like python, and there is a cool "* parameter ":

Code
1 Def Stepper (what, = 1 ):
2 What + = By
3 Return What
4
5 Def Average (first, * Rest ):
6 Sum = First
7 ForValueInRest: Sum+ = Value
8 Result =   1.0   * Sum / (LEN (rest) +   1 )
9 Return Result

Of course, we can also use the param keyword to do this, but it is not as cool as Python, but it is enough :)

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.