CLR covariant, inverter, clr

Source: Internet
Author: User

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.

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.