R
raylopez99
What are these terms in my header anyway?
But more importantly for me, look at this code:
Lack of variance:
why a List<Banana> isn’t a List<Fruit>
Many people initially expect the following code to compile:
List<Banana> bananas = new List<Banana>();
List<Fruit> fruit = bananas;
//however, it fails to compile (from Jon Skeet's cheat sheet)
However, this should work, right??:
List <Fruit> mybaseFruit = new List <Fruit>();
class Banana: Fruit {}
Banana myBanana = new Banana(); //note, this is NOT a list, but an
instantiation of a derived class Banana having base class Fruit
mybaseFruit.Add(myBanana); //should work, right? Otherwise what's the
point of inheritance?
RL
But more importantly for me, look at this code:
Lack of variance:
why a List<Banana> isn’t a List<Fruit>
Many people initially expect the following code to compile:
List<Banana> bananas = new List<Banana>();
List<Fruit> fruit = bananas;
//however, it fails to compile (from Jon Skeet's cheat sheet)
However, this should work, right??:
List <Fruit> mybaseFruit = new List <Fruit>();
class Banana: Fruit {}
Banana myBanana = new Banana(); //note, this is NOT a list, but an
instantiation of a derived class Banana having base class Fruit
mybaseFruit.Add(myBanana); //should work, right? Otherwise what's the
point of inheritance?
RL