static void Main(string[] args) { // The list cannot work with the IPlate<IWaffle> anymore. So here comes IPlateNG to the rescue var plates = new List <IPlateNG>(); plates.Add(new HugePlate()); plates.Add(new SmallPlate()); IPlateNG aPlate = plates[0]; // And instead of calling to the GetMyWaffle method we can call to the GetWaffle in this case IWaffle aWaffle = aPlate.GetWaffle(); Console.WriteLine(aWaffle.Eat()); IPlate <FalafelWaffle> aSmallPlate = (SmallPlate)plates[1]; FalafelWaffle aFalafel = aSmallPlate.GetMyWaffle(); Console.WriteLine(aFalafel.Dinner()); }
static void Main(string[] args) { var plates = new List <IPlate <IWaffle> >(); plates.Add(new HugePlate()); plates.Add(new SmallPlate()); IPlate <IWaffle> aPlate = plates[0]; // Anyway, when you get a member of the collection you'll get the interface, not a concrete class (obviously). IWaffle aWaffle = aPlate.GetMyWaffle(); // So you cannot invoke any specifics (like Breakfast or Dinner) Console.WriteLine(aWaffle.Eat()); // But if you cast the member of the collection to the specific class (or interface) IPlate <FalafelWaffle> aSmallPlate = (SmallPlate)plates[1]; // Then you'll get the concrete class without casting again FalafelWaffle aFalafel = aSmallPlate.GetMyWaffle(); Console.WriteLine(aFalafel.Dinner()); }