A simple foreach
will do the trick (for readability, i've named the classes Foo and Bar)
myFoos = new List<Foo>();
foreach (Bar bar in myBars)
{
myFoos.Add((Foo)bar);
}
After question edit:
To convert a list of multiple child classes into a base class, I would create a class called BaseCollection, than would inherit from List, as usually some other operations are required of the lists as well. Some usefull methods might be:
public class BaseCollection : List<Base>
{
public static BaseCollection ToBase<T>(IEnumerable<T> source) where T: Base
{
BaseCollection result = new BaseCollection();
foreach (T item in source)
{
result.Add(item);
}
return result;
}
public static List<T> FromBase<T>(IEnumerable<Base> source) where T: Base
{
List<T> result = new List<T>();
foreach (Base item in source)
{
result.Add((T)item);
}
return result;
}
public static List<T> ChangeType<T, U>(List<U> source) where T: Base where U:Base
{
List<T> result = new List<T>();
foreach (U item in source)
{
//some error checking implied here
result.Add((T)(Base) item);
}
return result;
}
....
// some default printing
public void Print(){...}
....
// type aware printing
public static void Print<T>(IEnumerable<T> source) where T:Base {....}
....
}
That would enable me to easily cast any descendant to and from base class collections, and use them like this:
List<Child> children = new List<Child>();
children.Add(new Child { ID = 1, Name = "Tom" });
children.Add(new Child { ID = 2, Name = "Dick" });
children.Add(new Child { ID = 3, Name = "Harry" });
BaseCollection bases = BaseCollection.ToBase(children);
bases.Print();
List<Child> children2 = BaseCollection.FromBase<Child>(bases);
BaseCollection.Print(children2);