Home > Article > Backend Development > Detailed introduction to C# generic parameter conversion
This article introduces the relevant knowledge of C# generic parameter conversion, which has a good reference value. Let’s take a look at it with the editor.
Objects generated by different parameter types of generics are independent of each other.
//如 Tuple<string> ts; Tuple<object> to; //ts to 是两个类型的对象。
Many times, we want to implement to = ts operation, why? Because it looks like it should be.
In order to achieve this goal, it is necessary to solve the "problem of generic parameter conversion". The knowledge point of this problem is the in out generic variant. To be honest, the problem itself isn't difficult, it's just very unintuitive and easy to forget.
First of all, in order to realize to = ts, there is actually a prerequisite, that is, this parameter can only be used on the "return type".
//如 delegate object FuncObj(); FuncObj func = ()=>"string";
The reason why func succeeds is because string can be converted into object. When the "user" calls func, what he hopes to get is the object object, and string is also an object object, so there is no problem.
The key here is to learn to analyze problems from a "user" perspective.
//A delegate void FuncObj2(object obj); FuncObj2 func2 = (string str)=>{}; //B delegate void FuncStr(string str); FuncStr func3 = (object obj)=>{};
Analyze these two sets of codes, which one is more reasonable?
From the user's perspective, it uses func2 and func3
When users use func2, the object passed must be object, but the actual function processed is (string)=>{ }, object cannot be converted into string, so it is very unreasonable.
When users use func3, the object passed can only be string, and the function actually processed is (object)=>{}. String can be converted into object, so it is reasonable.
Of course, these two sets of codes are not valid because the function parameter types do not match.
But generics provide a method to implicitly convert objects with mismatched types! The logic it implements is analyzed above.
//out 修饰返回类型 delegate ResultType FuncOut<out ResultType>(); //in 修饰参数类型 delegate void FuncIn<in ParamType>(ParamType param); //这是一开始我们想做到的目标 FuncOut<object> fun4 = () => "string"; //这个效果恰好相反 FuncIn<object> funcobj = (object obj) => { }; FuncIn<string> fun5 = funcobj; //注意,泛型变体一般只能和泛型变体之间隐式转换 //lambda表达式会自动转换成参数相同的泛型变体,但无法接着做变体之间的隐式转换,所以需要funcobj来过渡
out modifies the return type, and in modifies the parameter type, which is quite vivid, but pay attention to the generic in parameter, which is exactly 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 in generics. I hope readers can understand this.
Summary:
out : to = ts; in : ts = to;
No modification: to, ts are completely independent.
---------------------------(Remark)-------------- ------------------
out parameters: can only be used in return types.
in parameter: can only be used in parameters.
No modification: any position.
--------------------------(Note 2)------------- ------------------
In and out generic parameters can only be used on delegates and interfaces.
//综合运用 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 introduction of C# generic parameter conversion. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!