// The old way private static void TypeCastingWithoutPatternMatching(Storage storage) { if (storage == null) { throw new ArgumentNullException(); } if (storage is UsbKey) { UsbKey usbKey = (UsbKey)storage; if (usbKey.IsPluggedIn) { usbKey.Unload(); Console.WriteLine("USB Drive Unloaded."); } else { throw new NotImplementedException(); } } else if (storage is Dvd) { } else { throw new NotImplementedException(); } }
// Pro účely Dema // Jinak zde by rozhodně byla lepší abstraktní metoda Eject na Storage public static void Eject_WithoutPatternMatching(Storage storage) { if (storage == null) { throw new ArgumentNullException(); } if (storage is UsbKey) { UsbKey usbKey = (UsbKey)storage; if (usbKey.IsPluggedIn) { usbKey.Unload(); Console.WriteLine("USB Drive Unloaded."); } } else if (storage is DVD) { DVD dvd = (DVD)storage; if (dvd.IsInserted) { // ... } } }