Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I instantiate a IList<T> of nested IList<T>?

Tags:

c#

list

generics

I am trying to create a list of lists but am having trouble instantiating the list.

IList<IList<T>> allLists = List<List<T>>();

I am getting a compile error with this line.

like image 497
John Egbert Avatar asked Sep 13 '10 16:09

John Egbert


1 Answers

You'll have to instantiate a List of IList<T>, not a List of List<T>.

The reason is that by implementing IList<IList<T>> you are saying "Here is a list of some kind in which you can get or insert anything that implements IList<T>". Only objects of type List<T> can be inserted into List<List<T>>, so it is not allowed.

IList<IList<T>> allLists = new List<IList<T>>();
like image 64
Greg Avatar answered Oct 16 '22 14:10

Greg