Z. Margeta

Imagine the following situation.

Let's say you have a class A, which derives from IA. Let's also say that you have a collection class B which derives from IB.

IB derives from IEnumerable<IA>, and B derives from IEnumerable<A>.

Why isn't it possible to cast (IB)(B) without (implicitly) implementing the IEnumerable<IA> in B

Doesn't B implement IEnumerable<IA> implicitly since it implements IEnumerable<A> (A is derived from IA)

If you managed to stay with me this far, is there maybe a different way to do this

Re: Visual C# Language Casting question

Peter Ritchie

Generics aren't covariant, meaning you can't cast one generic to another despite using the same templated class (generic). See http://www.fotia.co.uk/fotia/DN.01.CoVariantGenericList.aspx for an overview of Generics and covariance and the problems that would arise if you could do what you're asking to do.