I have a bunch of lists of objects that implement an interface I
. I want to make a List of these Lists and work on them as a singular object. I've tried making a List<List<I>>
and adding my other lists to it, which in my mind makes sense since the lists I am adding contain objects that implement I
, but the compiler isn't letting me do this.
Example:
public interface I
{
//stuff
}
public class A : I
{
//...
}
public class B : I
{
//...
}
//elsewhere
public class C
{
List<A> ListOfA;
List<B> ListOfB;
List<List<I>> ListOfLists;
public C()
{
ListOfA = new List<A>();
ListOfB = new List<B>();
ListOfLists = new List<List<I>>();
ListOfLists.Add(ListOfA); //nope
ListOfLists.Add(ListOfB); //nope
}
}
I have two questions, the first being why is this not allowed behaviour? The second is how can I accomplish what I'm looking for?
For clarification, when I try to add the lists to ListOfLists
I get "cannot convert from List to List" where T : I
. I'm using .NET 3.5, and no I cannot upgrade to a later version.