0

This is a simplified version of my code:

using System.Collections.Generic;

public abstract class FruitBox<T>
{
    public T item;

    public static T ChooseFirst(List<FruitBox<T>> fruitBoxes)
    {
        return fruitBoxes[0].item;
    }
}

public class Apple
{
}

public class AppleBox : FruitBox<Apple>
{
}

public class FruitShop
{
    List<AppleBox> appleBoxes = new List<AppleBox>();

    public void Main()
    {
        AppleBox appleBox = new AppleBox();
        appleBoxes.Add(appleBox);

        AppleBox.ChooseFirst(appleBoxes); // => Error here
    }
}

I have an error in the line:

AppleBox.ChooseFirst(appleBoxes);

cannot convert from System.Collections.Generic.List<AppleBox> to System.Collections.Generic.List<FruitBox<Apple>>

I tried:

AppleBox.ChooseFirst((List<FruitBox<Apple>>)appleBoxes);

But same error.

How do I have to proceed?

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
fguillen
  • 36,125
  • 23
  • 149
  • 210

2 Answers2

1

The reason for such behaviour is explained here. In short - classes do not support variance in C# and List<AppleBox> is not List<FruitBox<Apple>>.

What you can do:

  • "convert" collection (actually create a new one):

with OfType<>().ToList()

AppleBox.ChooseFirst(appleBoxes.OfType<FruitBox<Apple>>().ToList())

or just ToList

AppleBox.ChooseFirst(appleBoxes.ToList<FruitBox<Apple>>())
public abstract class FruitBox<T>
{
    public T item;

    public static T ChooseFirst(IEnumerable<FruitBox<T>> fruitBoxes)
    {
        return fruitBoxes.First().item;
    }
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
0

You will have to hold the reference of the derived class into the base class variable

List<FruitBox<Apple>> appleBoxes = new List<AppleBox>();

FruitBox<Apple> appleBox = new AppleBox();
appleBoxes.Add(appleBox);

appleBox.ChooseFirst(appleBoxes);
Abhay Prince
  • 2,032
  • 1
  • 15
  • 17
  • This has the same problem as OP. `List> appleBoxes1 = new List()` will not compile cause `List` is not `List>`. – Guru Stron Mar 03 '22 at 20:01