I’m unclear on the differences between
public class MyVehicleCollection<T> where T : Vehicle
{
private List<T> listofVehicles = new List<T>();
public void AddVehicle(T v) { listofVehicles.Add(v); }
}
and
public class MyVehicleCollection<Vehicle>
{
private List<Vehicle> listofVehicles = new List<Vehicle>();
public void AddVehicle(Vehicle v) { listofVehicles.Add(v); }
}
I don’t see why in both cases its only possible to add types derived from Vehicle to the collection. I also don’t see why i’d need to create additional collection classes for containing Motobike : Vehicle in the non-generic case.
Many thanks in advance.
Your second form is not bound to Vehicle at all:
From your description, you don’t seem to need generics at all.
You don’t :