Matrix
implements IEnumerable<Vector>
but not IEnumerable<IEnumerable<float>>
. Either let it implement IEnumerable<IEnumerable<float>>
as well or cast it to the right type:
IEnumerable<IEnumerable<float>> floatEnumerationEnumeration =
matrix.Cast<IEnumerable<float>>();
Note that if B
derives from A
then this does not mean that T<B>
also derives from T<A>
. In fact T<A>
and T<B>
are considered to be two distinct unrelated types.
It is not easy to see why, but let's take another example:
class Person
{
public string Name { get; set; }
}
class Student : Person
{
public char Grade { get; set; }
}
Now let's assume that this was allowed:
List<Student> students = new List<Student>();
List<Person> persons = students;
persons.Add(new Person()); // Oops!
The the underlying list is still a students list and it does not allow you to add objects of type Person
!
Okay your example is different and it should work if you know how the types are implemented. But C# sees only abstract syntax constructs and does know what the types are really intended to do. The compiler is not able to say: "Okay IEnumerable<T>
does not have an Add
method, therefore this problem will not occur". Therefore generic types cannot automatically be co- or contravariant.