Covariance and anti-change of interface

Source: Internet
Author: User

Covariant and anti-change refers to the conversion of the type of the parameter and the return value.

If a generic type is labeled with an out keyword, the generic interface is covariant. This also means that the return type can only be t. The interface index and type T are covariant and return this type from a read-only indexer;

 Public Interface iindex< out t>{    T thisiint index{get;}     int Count {get;}}

If a generic type is labeled with the IN keyword, the generic interface is immutable, and the interface can only use the generic type T as the input to its method.

 Public Interface idisplay< in t>{ID Show (T item);}

If a system indexer is used on the interface iindex, the generic type T is passed to the method and retrieved from the method, which cannot be implemented by covariance-the generic type must be defined as immutable. If the out and in labels are not applicable, the type can be defined as immutable.

Covariance and anti-change of interface

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.