John Egbert
John Egbert

Reputation: 5756

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

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.

Upvotes: 10

Views: 1805

Answers (1)

Greg
Greg

Reputation: 23463

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>>();

Upvotes: 15

Related Questions