Let's say I have a generic list of Fruit
(List<Fruit> fruits = new List<Fruit>()
). I then add a couple of objects (all derived from Fruit
) - Banana
, Apple
, Orange
but with different properties on the derived objects (such as Banana.IsYellow
).
List<Fruit> fruits = new List<Fruit>();
Banana banana1 = new Banana();
Banana banana2 = new Banana();
Apple apple1 = new Apple();
Orange orange2 = new Orange();
fruits.Add(banana1);
fruits.Add(banana2);
fruits.Add(apple1);
fruits.Add(orange1);
Then I can do this:
foreach(Banana banana in fruits)
Console.Write(banana.IsYellow);
But at execution time of course this is not valid because there is no IsYellow
-property on the Apple and Orange objects.
How do I get only the bananas, apples, oranges etc from the List<Fruit>
?