I don’t understand why ‘x’ below converts, but ‘y’ and ‘z’ do not.
var list = new List<List<int>>();
IEnumerable<List<int>> x = list;
List<IEnumerable<int>> y = list;
IEnumerable<IEnumerable<int>> z = list;
Does the new covariance feature simply not work on generics of generics or am I doing something wrong? (I’d like to avoid using .Cast<> to make y and z work.)
“z” is fine in C# 4.0,
IEnumerable<T>is covariant.List<T>is however not, you cannot make “y” work.Intuitively, if it were then this would be valid:
Which breaks the promise that “list” can only contain
List<int>elements.