A detailed introduction to C # generic parameter conversions

Source: Internet
Author: User
This paper introduces the knowledge of C # generic parameter conversion, which has a good reference value, so let's take a look at the small series below.

The objects that are generated by the different parameter types of generics are independent of each other.

such as tuple<string> TS; Tuple<object> To;//ts To is a two-type object.

Many times, we want to implement to = TS This kind of operation, why? Because it seems to be so.

In order to achieve this, we need to solve the problem of "generic parameter Transformation", the knowledge point of this problem is in-out generic variant. Frankly speaking, the problem itself is not difficult, but very not intuitive, it is easy for people to forget.

First of all, in order to implement to = TS, there is actually a precondition, that is, the parameter can only be used on the "return type".

such as delegate object Funcobj (); Funcobj func = () = "string";

The reason why Func succeeds is because string can be converted to object. When "user" calls Func, he wants to get the object, and string is the object, so there is no problem.

The key here is to learn to use the "user" perspective to analyze the problem.

adelegate void FuncObj2 (object obj); FuncObj2 FUNC2 = (string str) =>{};//bdelegate void Funcstr (string str); Funcstr func3 = (Object obj) =>{};

Which of these two sets of code is more reasonable?

At the user's perspective, it uses FUNC2 and func3

Users use Func2 to pass objects that are necessarily object, but the actual processing of the function is (string) =>{},object cannot be converted to string, so it is unreasonable.

It is reasonable for a user to use FUNC3 to pass an object that is only a string, while the actual function being processed is (object) =>{},string can be converted to object.

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

But generics provide a way to make implicit conversions between objects of type mismatch! The logic that it implements is the above analysis.

Out modifier return type delegate Resulttype funcout<out resulttype> ()//in modifier parameter type delegate void Funcin<in paramtype> ( Paramtype param);//This is the first goal we want to achieve funcout<object> Fun4 = () = "string";//This effect is exactly the opposite funcin<object> Funcobj = (Object obj) = {}; funcin<string> Fun5 = funcobj;//Note that generic variants can generally only be implicitly converted between generic variants//lambda expressions are automatically converted to generic variants with the same parameters, but cannot be followed by implicit conversions between variants. So we need funcobj to transition.

The out-decorated return type, in-modifier parameter type, is still quite graphic, but be aware of the generic in parameter, just the opposite of the out parameter.

At first we want to implement to = TS, just to see half of the problem, in fact generics are the possibility of having TS = to, and hopefully the reader will understand that.

Summarize:

out:to = Ts;in:ts = to;

No modification: To,ts is completely independent.

---------------------------(note)-------------------------------
Out parameter: can only be used in return type.

In parameter: can only be used in parameters.

No decorations: any position.

---------------------------(note 2)------------------------------

In, out generic parameters can only be used on delegates and interfaces.

Integrated use of Delegate Resulttype Funcinout<in Paramtype, out resulttype> (paramtype param); Funcinout<object, string> funcobj2 = (Object obj) = "string";  funcinout<string, object> func6 = funcobj2;

The above is the detailed description of C # generic parameter conversion, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.