Home > Blockchain >  C#, How to pass a List of a Derived class to a method that receives a List of the Base class?
C#, How to pass a List of a Derived class to a method that receives a List of the Base class?

Time:03-04

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?

CodePudding user response:

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;
    }
}

CodePudding user response:

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);
  • Related