interface IVehicle
{
void DoSth();
}
class VW : IVehicle
{
public virtual void DoSth() { ... }
}
class Golf : VW { }
class Lupo : VW
{
public override void DoSth()
{
base.DoSth();
...
}
}
in my code i have:
List<VW> myCars = new List<VW>();
myCars.Add(new Golf());
myCars.Add(new Lupo());
now i want to evaluate if i have a list of vehicles. something like:
if(myCars is List<IVehicle>)
{
foreach(IVehicle v in myCars)
v.DoSth();
}
how can i do this? the is-operator on the generic list does not work. is there another way?
VWobjects is a list ofIVehicleobjects seems silly, sinceVWinherits fromIVehicleamd you are therefore writingif(true). Besides, sinceVWinherits fromIVehicleandmyCarsis aList<VW>,foreach (IVehicle v in myCars)will simply work.