IList<Type> to IList<BaseType>
.net, c#, inheritance
Solution
Use IEnumerable`<T>`.Cast :
IList<Vehicle> vehicles = cars.Cast<Vehicle>().ToList();
Alternatively, you may be able to avoid the conversion to List depending on how you wish to process the source car list.
Problem
I have a few classes: ``` class Vehicle { } class Car : Vehicle { } ``` I have a list of the derived class: `IList<Car> cars;` I would like to convert the list to its base class, and have tried: `IList<Vehicle> baseList = cars as IList<Vehicle>;` But I always get `null`. Also ``` cars is IList<Vehicle> evaluates to be false. ``` Granted, I can add the items to a list if I do the following: ``` List<Vehicle> test = new List<Vehicle> (); foreach ( Car car in cars ) { test.Add(car); } ``` And I get my list, but I know there has to be a better way. Any thoughts?