C # 4 covariance supports generic nesting?

I do not understand why the "x" below converts, but the "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;

Is the new covariance function simply unworkable for generic generics, or am I doing something wrong? (I would like to avoid using .Cast <> to make y and z work.)

+5
source share
2 answers

"z" excellent in C # 4.0 IEnumerable<T>is covariant. List<T>however, you cannot make "y" work.

Intuitively, if it were then it would be true:

List<IEnumerable<int>> y = list
y.Add(new Stack<int>());

, "" List<int>.

+7

. -, , , "int", .

-, :

var list = new List<List<String>>();
IEnumerable<IEnumerable<object>> z = list; 

List of List IEnumerable IEnumerables , . .

+1

All Articles