You have a base class
Mammal and a derived class
public abstract class Mammal
public class Dog : Mammal
You create a list of dogs
You want to have a list of mammals
List<Mammal> that is built from the list of dogs. If you try either an implicit or explicit cast you will be told that it isn't possible!
This little function will create a list of mammals from a list of dogs.
public static List<TBase> DerivedConverter<TBase, TDerived>(List<TDerived> derivedList)
where TDerived : TBase
new Converter<TDerived, TBase>(delegate(TDerived derived)
To use it:
List<Base> listBase = DerivedConverter<Base, Derived>(listDerived);
In our scenario
Base would be
Derived would be
You could use the same function for
Cat etc, or any other derived class list to base class list without having to write a new converter.