Generic parameter conversion

Source: Internet
Author: User

Generic parameter conversion

Objects generated by different parameter types of generics are independent of each other.

// Tuple <string> ts; Tuple <object> to; // ts to is an object of two types.

Many times, we want to implement the to = ts operation. Why? Because it seems like this.

To achieve this goal, we need to solve the "problem of generic parameter conversion". The knowledge point of this problem is the in out generic variant. To be honest, this problem is not difficult, but is very intuitive and easy to forget.

First, in order to implement to = ts, there is actually a premise that this parameter can only be used for "return type.

// For example, delegate object FuncObj (); FuncObj func = () => "string ";

Func succeeds because string can be converted to object. When the "user" calls func, what you want is the object, and string is also the object, so there is no problem.

The key here is to learn to analyze problems from the perspective of "users.

//Adelegate void FuncObj2(object obj);FuncObj2 func2 = (string str)=>{};//Bdelegate void FuncStr(string str);FuncStr func3 = (object obj)=>{};


Which of the two groups of code is more reasonable?

From the user perspective, it uses func2 and func3

When you use func2, the object to be passed must be an object, but the actual processing function is (string) =>{}. The object cannot be converted to a string, so it is unreasonable.

When you use func3, the passed object can only be string, while the actually processed function is (object) =>{}. string can be converted to object, so it is reasonable.

Of course, these two groups of Code are not valid because the function parameter types do not match.

However, generics provide a method for implicit conversion between objects with mismatched types! The implementation logic is analyzed above.

// Out modifier return type delegate ResultType FuncOut <out ResultType> (); // in modifier parameter type delegate void FuncIn <in ParamType> (ParamType param ); // This is the target FuncOut <object> fun4 = () => "string"; // the opposite result is FuncIn <object> funcobj = (object obj) =={}; FuncIn <string> fun5 = funcobj; // note, generic variants can only be implicitly converted to generic variants. // lambda expressions are automatically converted to generic variants with the same parameters. However, implicit conversions between variants are not allowed, therefore, funcobj is required for transition.

The out parameter specifies the return type. The in parameter specifies the type of the parameter. However, you must note that the generic in parameter is the opposite of the out parameter.

At first we wanted to implement to = ts, but we only saw half of the problem. In fact, there is a possibility of ts = to for generics. I hope readers can understand this.

Summary:

Out: to = ts;

In: ts =;

Not modified: to, ts is completely independent.

--------------------------- (Remarks )-------------------------------
Out parameter: it can only be used in the return type.

In parameter: can only be used as a parameter.

Not modified: any location.

--------------------------- (Note 2 )------------------------------

In and out generic parameters can only be used on the delegate and interface.

// Use delegate ResultType FuncInOut <in ParamType, out ResultType> (ParamType param); FuncInOut <object, string> funcobj2 = (object obj) => "string "; funcInOut <string, object> func6 = funcobj2;

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.