public void Run() { IList <string> strings = new List <string>(); // not allowed // IList<object> objects = strings; // HEEY? IEnumerable <object> objects2 = strings; // Covariant => Allows casting to more generic // Contravariant => Allows casting to more specific // Invariant => Allows none var dogs = new Dog[] { new Dog { Age = 2 }, new Dog { Age = 3 } }; var myZoo = new MyZoo <Animal>(dogs); var oldest = myZoo.Oldest(); Console.WriteLine(oldest); Animal cat = new Cat { Age = 12 }; IWriteOnlyList <Dog> dogList = new WriteOnlyList <Animal>(); dogList.Add(new Dog { Age = 7 }); }
static void Main(string[] args) { AirAnimals[] airAnimals = new AirAnimals[] { new Crane { Name = "Crane1", Age = 3, Energy = 8, Food = "Fish", Gender = Gender.Male, Color = "Blue", BodyTemperature = 50, Wings = true, Feathers = true, CanFly = true, }, new Crow { Name = "Crow1", Age = 1, Energy = 10, Food = "Worm", Gender = Gender.Female, Color = "Black", BodyTemperature = 50, Wings = true, Feathers = true, CanFly = true, }, new Eagle { Name = "Eagle", Age = 2, Energy = 6, Food = "Frog", Gender = Gender.Male, Color = "Black", BodyTemperature = 45, Wings = true, Feathers = true, CanFly = true, }, new Falcon { Name = "Falcon", Age = 5, Energy = 8, Food = "Bird", Gender = Gender.Male, Color = "White", BodyTemperature = 45, Wings = true, Feathers = true, CanFly = true, } }; LandAnimals[] landAnimals = new LandAnimals[] { new Muridae { Name = "Muradie1", Age = 2, Energy = 4, Food = "Cheese", Gender = Gender.Male, Color = "Grow", BodyTemperature = 50, Fur = true, CanWalk = true, }, new Rabbit { Name = "Rabbit1", Age = 4, Energy = 6, Food = "Carrot", Gender = Gender.Female, Color = "Black", BodyTemperature = 47, Fur = true, CanWalk = true, }, }; MyZoo myZoo = new MyZoo(airAnimals, landAnimals); Console.WriteLine($"There are {landAnimals.Length + airAnimals.Length} animals in the zoo"); Console.WriteLine(new string('*', 50)); foreach (var item in landAnimals) { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(item); Console.WriteLine("Type FullName:" + item.GetTypes()); Console.WriteLine(new string('*', 50)); Console.ResetColor(); } foreach (var item in airAnimals) { Console.ForegroundColor = ConsoleColor.DarkCyan; Console.WriteLine(item); Console.WriteLine("Type FullName:" + item.GetTypes()); Console.WriteLine(new string('*', 50)); Console.ResetColor(); } }