Consider the following example:
class Base {}
class Derived : Base {}
class Test1
{
private List<Derived> m_X;
public IEnumerable<Base> GetEnumerable()
{
return m_X;
}
}
This compiles just fine, because IEnumerable<T>
is covariant in T
.
However, if I do exactly the same thing but now with generics:
class Test2<TBase, TDerived> where TDerived : TBase
{
private List<TDerived> m_X;
public IEnumerable<TBase> GetEnumerable()
{
return m_X;
}
}
I get the compiler error
Cannot convert expression type 'System.Collection.Generic.List' to return type 'System.Collection.Generic.IEnumerable'
What am I doing wrong here?