A co-worker has an object that has a bunch of generic list collections on it. Each type in the list implements a given interface. He wants to be able to create a list that contains all the other lists so he can loop through and call the method the objects implement. Below is a simple example.
List<Dx> dxs = new List<Dx>();
dxs.Add(new Dx());
dxs.Add(new Dx());
List<Proc> procs = new List<Proc>();
procs.Add(new Proc());
List<List<IClean>> lists = new List<List<IClean>>();
lists.Add(procs); // Error here
lists.Add(dxs); // Error here
foreach (List<IClean> list in lists)
{
foreach (IClean i in list)
{
i.Clean();
}
}
Dx and Proc both implement IClean. This is the goal. Is something like this possible? Or, is this bad programming?
.NET Version 4.0
You could just have a single
List<IClean>that hold both implementations ofIClean.Furthermore, if you actually need to maintain two separate implementation lists and then process them together, you could do something like this: