17. C # type judgment and heavy load decision-making (Chapter 9 9.4 ),

Source: Internet
Author: User

17. C # type judgment and heavy load decision-making (Chapter 9 9.4 ),

Let's end Chapter 9 today, which we often ignore, but the compiler will help us complete "type judgment and heavy load decision-making" and understand how the compiler can help us complete it, I believe that it will be clearer when writing code to avoid compilation errors and troubleshooting, making development more powerful.

We know that implicit type arrays and method group conversion to delegate types require type inference, but it is extremely complicated to convert method groups as parameters of other methods, let's take a step-by-step look at how the compiler makes some inferences for us.

1 // define a generic method with parameters of the TInput type and a Func <TInput, TResult> type Delegate of the 2 public static TResult Get <TInput, TResult> (TInput input, func <TInput, TResult> func) 3 {4 return func (input); 5} 6 static void Main (string [] args) 7 {8 var a = Get ("111", x => int. parse (x); 9 // The first parameter is a string, so it can be inferred that the TInput type has implicit conversion to the string type 10 // The method signature is: public static TResult Get <string, TResult> (string input, Func <string, TResult> func) 11 // The second parameter is a Lambda expression. The returned type is an int, so it can be inferred that the TResult type has an implicit conversion to the int Type 12 // The method signature is: public static int Get <string, int> (string input, Func <string, int> func) 13 // according to the above steps, we infer TInput and TResult types 14}

  Return type set

  From the code above, we can see that the TResult type is inferred by the return value type of the Lambda expression. What is the case if I have multiple return types? See the following code.

1 var B = Get (1111, delegate (int x) {2 if (x = 1111) 3 {4 return "1111 "; 5} 6 else 7 {8 return new object (); 9} 10}); 11 // It can be inferred from the implicit conversion from the TInput type to the int type, TInput is int12 // and the return type has two types: string and object, but string has implicit conversion to object, then TResult is of the object Type 13 // the compiler will help us put all the return types into a set and judge the types in the set, determines whether a type is available by implicit conversion of other types. This type is used as the type of the returned value.

 

  How to select the correct overload

  If the overload has the ambiguity, the compilation will definitely fail. Either, You can forcibly convert a parameter to conform to the signature of a method, or modify the overload method. For the parameter or return type, refer to the following:

Converting from any type to itself is considered better than "converting to other types", such

1 static void Debug0 (int x) {} 2 static void Debug0 (double x) {} 3 4 Debug0 (5); // static void Debug0 (int x) 5 Debug0 (5.0); // static void Debug0 (double x) 6 // int has an implicit conversion to double, so it has ambiguity, but according to int => int Is Better Than int => double, so use static void Debug0 (int x)

 

Similarly, the return type acts on the delegate or Lambda expression, such

1 static int Debug1 (Func <int> func) {2 return func (); 3} 4 5 static double Debug1 (Func <double> func) 6 {7 return func (); 8} 9 10 Debug1 () => 1.1); 11 Debug1 (delegate () {return 1.1 ;}); 12 // Based on Lambda expressions and anonymous methods, Debug1 (() => 1.1) and Debug1 (delegate () {return 1.1;}) return the double type. Lambda expressions and anonymous methods are converted to Func <double> type instances, the call is static double Debug1 (Func <double> func) 13 Debug1 () => 1); 14 Debug1 (delegate () {return 1 ;}); 15 // Based on Lambda expressions and anonymous methods, the types returned by Debug1 () => 1) and Debug1 (delegate () {return 1 ;}) are int, lambda expressions and anonymous methods are converted to Func <int> type instances, and static double Debug1 (Func <int> func) is called)

Please make an axe.

 

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.