C # method overloads, optional parameters, named parameters.

Source: Internet
Author: User

In fact there is nothing to say oh, C + + syntax is similar. Look at the code first.

Class Program {public    static void Test (int a) {        Console.WriteLine ("Only one parameter a=={0}", a);    }    public static void Test (int a, int b = 5) {        Console.WriteLine ("With two parameters A=={0},b=={1}", A, b);    }    static void Main () {        Test (5);        Console.ReadLine ();    }}



Method overloading, optional parameter two techniques are used together, it is difficult to see the final call is the method. A call applies to a method only if all parameters (except the optional parameter) have exactly one argument and the parameter has a compatible type. Although this limits the number of callable methods, the lack of uniqueness is represented. To further differentiate the method, the compiler simply displays the identified parameters with the caller, ignoring the OH optional arguments not specified by the caller. Therefore, if a method has optional parameters, resulting in two methods are applicable, the compiler final draft selected is a method without optional parameters. As reflected in this code.


Method parsing:

When the compiler must choose from a range of applicable methods that best fits a particular invocation method, it chooses the one that has the most specific parameter type. If there are two applicable methods. Each requirement implicitly converts an argument to a formal parameter type, and the compiler ultimately chooses the implementation of that method of the more specific derived type.

For example:

If the caller passes an int, the method that accepts the double takes precedence over the method that accepts the object, because double is more specific than object because there is not a double object, but there is no double with the object, So double is more specific.

If there are multiple applicable methods, but cannot pick out the most unique, the compiler will error, indicating the existence of ambiguity.

These methods are as follows:

static void Method (Object thing) {};

static void Method (double thing) {};

static void Method (Long thing) {};

static viod Method (int thing) {};

Call Method (42) is parsed into int because there is a method that exactly matches the argument type to the new parameter type, and if the method is removed, overload resolution chooses the long version, because Long is a more specific version than double and object.

The C # specification contains additional rules to determine the hermit transitions between byte,ushort,uint,ulong and other numeric types, but it is best to use display transformations when writing programs to make it easier for others to understand which target method you want to invoke.


Named parameters

Class Program {public    static void Test (int a, int b) {        Console.WriteLine ("A=={0},b={1}", A, b);    }    static void Main () {        Test (b:5, a:3);        Console.ReadLine ();    }}


Named parameters are a great convenience for methods that contain optional parameters.

But sacrificing the flexibility of the interface to treat parameter names as part of the API, if version compatibility between APIs is important, avoid changing the parameter names.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # method overloads, optional parameters, named parameters.

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.