Several Problems of generic interface covariant Inverter

Source: Internet
Author: User

View Original text on the Internet

1. What are changes and inverters?

Assume that TSub is a subclass of TParent.
Covariant: If a generic interface IFoo <T> and IFoo <TSub> can be converted to IFoo <TParent>, we call this process a covariant, IFoo supports covariant of parameter T.
Inverter: If a generic interface IFoo <T> and IFoo <TParent> can be converted to IFoo <TSub>, we call this process an inverter. IFoo supports parameter T inversion.

2. Why is there a need for covariant and inverter?

Generally, only objects with inheritance relationships can undergo implicit type conversion, such as Base B = new sub ().
Covariant and inverter can enable implicit type conversion between more types and ensure type security.

3. Why do generic interfaces introduce covariant and inverter?

At the same time, many interfaces only use type parameters for parameters or return values. Therefore, more flexibility is available in the use of the generic model after the coordination and inverter are supported.

4. Why can only parameters supporting covariant be used for return values of methods? Which of the following parameters can be used only for method parameters?

"TParent cannot be safely converted to TSub", which is the common cause of these two problems.
We define an interface IFoo.
 

Interface IFoo <T>
{
Void Method1 (T param );
T Method2 ();
}

Let's take a look at the covariant process: IFoo <TSub> converts to IFoo <TParent>.

Method1: replace TSub with TParent. Method1 obviously has a conversion from TParent to TSub.

Method2: the return value type is changed from TSub to TParent, which is type-safe.

Therefore, parameters supporting covariant can only be used in return values of methods.

Let's take a look at the inverter process: IFoo <TParent> converts to IFoo <TSub>.

Method1: replace TParent with TSub. Method1 converts TSub to TParent, which is type-safe.

Method2: it is unsafe to change the return value type from TParent to TSub.

Therefore, parameters supporting inverter can only be used in method parameters.


5. Does the generic interface support covariant, inverter, and comparison between non-convariant and inverter?

This is actually a supplement to the three questions.

Define an interface IFoo, neither supporting covariant nor inverter.

Interface IFoo <T>
{
Void Method1 (T param );
T Method2 ();
}

Implementation interface IFoo

Public class FooClass <T>: IFoo <T>
{
Public void Method1 (T param)
{
Console. WriteLine (default (T ));
}
Public T Method2 ()
{
Return default (T );
}
}

Defines an interface that supports covariant T.

Interface IBar <out T>
{
T Method ();
}

Implementation interface IBar

Public class BarClass <T>: IBar <T>
{
Public T Method ()
{
Return default (T );
}
}

Define an interface IBaz that supports parameter T Inversion

Interface IBaz <in T>
{
Void Method (T param );
}

Implementation interface IBaz

Public class BazClass <T>: IBaz <T>
{
Public void Method (T param)
{
Console. WriteLine (param. ToString ());
}
}

Define two types with inheritance relationships: IParent and SubClass.

Interface IParent
{
Void DoSomething ();
}
Public class SubClass: IParent
{
Public void DoSomething ()
{
Console. WriteLine ("SubMethod ");
}
}

IFoo and IBar are used according to the covariant logic.

// IFoo does not support covariant of parameter T
IFoo <SubClass> foo_sub = new FooClass <SubClass> ();
IFoo <IParent> foo_parent = foo_sub; // compilation Error

// IBar supports covariant of parameter T
IBar <SubClass> bar_sub = new BarClass <SubClass> ();
IBar <IParent> bar_parent = bar_sub;

Foo_parent = foo_sub will prompt the compile-time error "the type" IFoo <SubClass> "cannot be implicitly converted to" IFoo <IParent> ". There is an explicit conversion (is forced conversion missing ?)"

 

IFoo and IBaz are used according to the inverter logic. // IFoo is incompatible with parameter T inverters
IFoo <IParent> foo_parent = null;
IFoo <SubClass> foo_sub = foo_parent; // compilation Error

// IBaz is compatible with parameter T inverters
IBaz <IParent> baz_parent = null;
IBaz <SubClass> baz_sub = baz_parent;

Foo_sub = foo_parent will prompt the compile-time error "the type" IFoo <IParent> "cannot be implicitly converted to" IFoo <ISub> ". There is an explicit conversion (is forced conversion missing ?)"

 

6. How does. NET4.0 modify the IEnumerable interface?

Definition in 2.0:

Public interface IEnumerable <T>: IEnumerable
{
IEnumerator <T> GetEnumerator ();
}

Definition in 4.0:

Public interface IEnumerable <out T>: IEnumerable
{
IEnumerator <T> GetEnumerator ();
}

We can see that support for covariant is added in 4.0.

You can try it in two versions. The following statement will report an error in 2.0.

List <SubClass> subarr = new List <SubClass> ();
IEnumerable <IParent> parentarr = subarr;

 
Refer:

Http://technet.microsoft.com/zh-CN/library/dd799517
Http://www.cnblogs.com/Ninputer/archive/2008/11/22/generic_covariant.html
Http://www.cnblogs.com/idior/archive/2010/06/20/1761383.html
Http://www.cnblogs.com/artech/archive/2011/01/13/variance.html

 

 

 

 

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.