1

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:

enter image description here

It does compile on the authors website.

Doug Hauf
  • 3,025
  • 8
  • 46
  • 70
  • 1
    Read about covariance and contravariance on MSDN: http://msdn.microsoft.com/en-us/library/dd799517.aspx – Crono Apr 23 '14 at 14:57
  • because List does not inherits List even though Banana is a Fruit. – Fedor Hajdu Apr 23 '14 at 14:57
  • `Banana` and `Fruit` may be cast between each other but the same does not apply to `List` and `List`, they are their own separate objects that do not inherit from each other. So you cannot say "This fruitbowl is this bunch of bananas" because the bunch of bananas is not a bowl but you could say "I am going to add a banana to the fruit bowl" `fruitbowl.add(new Banana());` or `fruitbowl.addRange(bunchOfBananas);` – Amicable Apr 23 '14 at 14:58
  • You answered yourself with your code - `List` explicitly says it can never contain anything that isn't a banana. If it were possible to put it in a `List` variable, you could add an apple to it, and the contract would be broken - the `bunchOfBananas` would have an apple in it. Instead, you can create a copy of the list - `bunchOfBananas.OfType().ToList();` (the reverse works too, and it will only give you the fruits that are bananas - handy). – Luaan Apr 23 '14 at 15:02
  • Please provide a link to the website, either you are misinterpreting his words or the author is wrong. The code in the screenshot will not compile, even if what he was trying to do did work he never declares `using System.Collections.Generic` (I don't see a scroll bar to be able to scroll up) so all of those `List<>` objects should not be recognized. – Scott Chamberlain Apr 23 '14 at 15:30
  • The code in the screenshot won't compile either. – Lee Apr 23 '14 at 15:30
  • This is on a site that we use for training. I asked my supervisor and he looked and said that it was wrongly done in the video and that it should not compile. I thought that it should compile but you are right it will not compile. Others had asked the same question. – Doug Hauf Apr 23 '14 at 15:50

0 Answers0