static void Main(string[] args) { //UseDelegate.InvokeDelegate(); List <Mammal> zoo = new List <Mammal> { new Tiger("Vasily"), new Lion("Leo"), new Feline(Color.Dotted, "Jagg") }; foreach (Feline feline in zoo) { Tiger t = feline as Tiger; Console.WriteLine(t?.Name.ToString() ?? "Это - не тигр"); } }
static void Main(string[] args) { //Console.WriteLine("Main method in Register class ..."); Tiger[] tigers = new Tiger[] { new Tiger(42, 50, 7), new Tiger(101, 53, 3), new HelmetTiger(101010, 60, 6), new HelmetTiger(666, 59, 5, new Helmet(20, 2)) }; //Iterate through the array (foreach), print each tiger //Polymorphism / Polymorphie: Call subclass method via baseclass reference //Polymorphie nur mit dynamischer Bindung (= virtuelle Methoden)! foreach (Tiger tiger in tigers) { Console.WriteLine("{0}", tiger); //Test if current tiger is a HelmetTiger: //Console.WriteLine("Is HelmetTiger: {0}", tiger is HelmetTiger); //Try to cast from baseclass to subclass => down-cast HelmetTiger helmetTiger = tiger as HelmetTiger; if (helmetTiger != null) { Console.WriteLine("Is HelmetTiger: {0}", helmetTiger.TigerHelmet); } } //Create a new register from the existing array: Register reg = new Register(tigers); //Print second tiger from register via indexer: Console.WriteLine("Second tiger: {0}", reg[1]); Tiger firstSearchTiger = reg.FindTiger(666); Tiger secondSearchTiger = reg.FindTiger(777); Console.WriteLine("Tiger with ID 666: {0}", firstSearchTiger); Console.WriteLine("Tiger with ID 777: {0}", secondSearchTiger); }