C # Keywords Series 6 in (Generic Modifier) & amp; out (Generi

Source: Internet
Author: User

This article describes two generic parameter modifiers, in and out. The author does not understand these two keywords. To learn to use these two keywords, you must first understand the concepts of covariant and inverter. The simple definition of covariant and inverter is that the covariant means that the generic interface is converted from the subclass to the parent class, And the inverter means the conversion from the parent class to the subclass class. The following two conversions are covariant and invert: [csharp] IEnumerable <string> strs = null; IEnumerable <object> objs = null; objs = strs; // "subclass" to "parent class", that is, covariant strs = objs of the generic interface; // "parent class" to "subclass, that is, the inverter of the generic interface. This will report an error. The IEnumerable interface uses out to modify the generic parameters, and only supports covariant. Here, we only want to illustrate the difference between the two conversions, IEnumerable <string> and IEnumerable <object> are similar to parent classes and subclasses. In fact, they do not have any inheritance relationships and can be considered as two independent classes. At the beginning of. NET 4.0, conditional conversions between the above-mentioned covariant and the inverter are allowed. Here, the in or out keyword is used to modify the use range of the generic parameter T. You can see the IEnumerable statement [csharp] [TypeDependency ("System. SZArrayHelper ")] public interface IEnumerable <out T>: IEnumerable {IEnumerator <T> GetEnumerator ();} if the IEnumerable interface modifies generic parameters with in, the inverter (strs = objs;) can be compiled. So we can sum up that the out parameter can be used to modify the generic parameters, and the in parameter can be used to modify the generic parameters. Speaking of this, there is nothing to say about these two keywords. That's why. NET 4.0 began to introduce the covariant inverter. [Csharp] List <object> list = new List <object> (); list. addRange (strs); // if there is no covariant (implicit conversion of sub-classes to the parent class), this line of code returns the list error. addRange (objs); In the code above, if the IEnumerable interface does not use out to modify generic parameters, an error will be reported during compilation. In addition, you have to write a loop to convert strs to IEnumerable <object> one by one. To avoid this problem, you can directly convert the strs to avoid compilation errors. All the problems of covariant inversion actually come from a fundamental principle: subclasses can be implicitly converted to the parent class, and the parent class cannot be implicitly converted to the Child class. Finally, let's look at a complete example to see how to use in and out: [csharp] class Program {static void Main (string [] args) {IContravariant <Object> iobj1 = new Sample <Object> (); IContravariant <String> istr1 = new Sample <String> (); ICovariant <Object> iobj2 = new Sample <Object> (); ICovariant <String> istr2 = new Sample <String> (); istr1 = iobj1; // The in modifier implements covariant (implicit conversion of sub-classes to the parent class) iobj2 = istr2; // The out modifier implements inversion (implicit conversion of sub-classes to the parent class )}} // inverter interface. interface IContravariant <in A >{} // The covariant API. interface ICovariant <out R >{} // class for implementing the covariant inverter interface. class Sample <A>: IContravariant <A>, ICovariant <A> {}

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.