If I have 3 classes Fruit, Banana, and Apple. I make a List of Banana and then a List of Fruit but I want to set the bunchOfBananas to fruitBowl.
This works in the video but does not work on my program.
Question: The fruitBowl is the Parent class. The Banana is the child class of the Fruit class. Why can I not set the bunchOfBananas variable to the fruitBowl variable?
Code:
class Program
{
static void Main(string[] args)
{
List<Banana> bunchOfBananas = new List<Banana>();
List<Fruit> fruitBowl = bunchOfBananas; <-- Error
fruitBowl.Add(new Apple());
Console.ReadKey();
}
public class Fruit { }
public class Banana : Fruit { }
public class Apple : Fruit { }
}
Error:
Error 1 Cannot implicitly convert type 'System.Collections.Generic.List<ConsoleApplication1.Program.Banana>' to 'System.Collections.Generic.List<ConsoleApplication1.Program.Fruit>' C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 19 37 ConsoleApplication1
Screen Shot:
It does compile on the authors website.