CLR covariant, inverter, clr
Reading a book is a bit dizzy, and it is unclear whether the covariant or inverter is used.
Seeing the generic and delegate methods, we found that the co-variant and inverter here are different.
Generic inverters and collaborative changes:
If a return type can be replaced by its base class, the type supports covariant.
If a parameter type can be replaced by its derived class, this type supports invert.
Reference: http://www.cnblogs.com/IPrograming/p/4471130.html
The sample code is as follows:
public class BaseClass{ //...}public class DerivedClass : BaseClass{ //...}
IEnumerable<DerivedClass> d = new List<DerivedClass>();IEnumerable<BaseClass> b = d;
// IEnumerable <T> interface definition (supports covariant) public interface IEnumerable <out T>: IEnumerable
Inverter and coordination of delegation methods:
If a return type can be replaced by its derived class, this type supports covariant.
If a parameter type can be replaced by its base class, this type supports invert.
Public delegate object TestDelegate (string str); class MyClass {public string t1 (string s) {return "" ;}public object t2 (object s) {return "";} public string t3 (object s) {return "";} public int t4 (string s) // The value type is not allowed. Only the reference type supports covariant and inversion. {return 0 ;} public void main () {TestDelegate td1 = t1; TestDelegate td2 = t2; TestDelegate td3 = t3; TestDelegate td4 = t4; // compilation error, Type Error returned }}
I am also a little confused.
If you have any mistakes in reading and checking the information, please correct me.