17.c# type judgment and overload resolution (IX 9.4)

Source: Internet
Author: User

Today to end the Nineth chapter, chat we often ignore, but the compiler will help us to complete the "type judgment and overload decision", understand how the compiler helps us to complete, I believe in writing code will be more explicit, to avoid some compilation error, troubleshooting problems, let us develop more force.

We know that implicitly typed arrays and the conversion of method groups to delegate types require type inference, but it can be extremely complex to convert a method group as an argument to another method, and we take a step-by-turn look at how the compiler can help us make some inferences.

1 //defines a generic method with parameters of Tinput type and one func<tinput, tresult> type of delegate2  Public StaticTResult Get<tinput, tresult> (tinput input, Func<tinput, tresult>func)3 {4     returnfunc (input);5 }6 Static voidMain (string[] args)7 {8     varA = Get ("111", x =int. Parse (x));9     //The first argument is a string, so you can infer that the tinput type has an implicit conversion to the string typeTen     //The method signature is: public static TResult get<string, tresult> (string input, func<string, tresult> Func) One     //The second argument is a lambda expression, which is an int based on the returned type, so you can infer that the TResult type has an implicit conversion to type int A     //The method signature is: public static int get<string, int> (string input, func<string, int> Func) -     //based on the above steps, we infer the tinput and TResult types -}

  Return type Collection

  From the code above, you can see that the TResult type is inferred by the return value type of the lambda expression, so what if I have more than one return type? Take a look at the following code.

1 varb = Get (1111,Delegate(intx) {2     if(x = =1111)3     {4         return "1111";5     }6     Else7     {8         return New Object();9     }Ten }); One //It can be inferred that the Tinput type has an implicit conversion to the int type, then tinput is an int A //The return type has two, one is a string type, one is the object type, but the string type has an implicit conversion to the object type, then TResult is the object type - //The compiler helps us put all the return types into a collection, judging the types in the collection, and judging if there is one type that can be implicitly converted by the remaining other types, and the type as the return value.

  How to select the correct overload

  If the overload is ambiguous, the compilation must be too much, either casting a parameter to conform to the signature of a method, or modifying the overloaded method. Consider a parameter or return type, as follows:

From any type "converted to itself" is considered better than "convert to another type", as

1 Static voidDEBUG0 (intx) {}2 Static voidDEBUG0 (Doublex) {}3 4DEBUG0 (5);//static void Debug0 (int x)5DEBUG0 (5.0);//static void Debug0 (Double x)6 //Int has an implicit conversion to a double, so there is ambiguity, but according to Int=>int is better than int=>double, so choose to use the static void Debug0 (int x)

In the same way as the return type, used for delegates or lambda expressions, such as

1 Static intDEBUG1 (func<int>func) {2     returnfunc ();3 }4 5 Static DoubleDEBUG1 (func<Double>func)6 {7     returnfunc ();8 }9 TenDEBUG1 (() =1.1); OneDEBUG1 (Delegate() {return 1.1; }); A //based on lambda expressions and anonymous methods, DEBUG1 (() = 1.1) and Debug1 (delegate () {return 1.1;}) The returned type is a double type, and the lambda expression and the anonymous method are converted to instances of the func<double> type, and the static double Debug1 (func<double> Func) is called. -DEBUG1 (() =1); -DEBUG1 (Delegate() {return 1; }); the //based on lambda expressions and anonymous methods, DEBUG1 (() = 1) and Debug1 (delegate () {return 1;}) The returned type is of type int, and the lambda expression and the anonymous method are converted to instances of the func<int> type, and the static double Debug1 (func<int> Func) is called.

Please treatise.

17.c# type judgment and overload resolution (IX 9.4)

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.