Reputation: 38822
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>
toSystem.Collections.Generic.List<FruitBox<Apple>>
I tried:
AppleBox.ChooseFirst((List<FruitBox<Apple>>)appleBoxes);
But same error.
How do I have to proceed?
Upvotes: 0
Views: 556
Reputation: 142743
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:
with OfType<>().ToList()
AppleBox.ChooseFirst(appleBoxes.OfType<FruitBox<Apple>>().ToList())
or just ToList
AppleBox.ChooseFirst(appleBoxes.ToList<FruitBox<Apple>>())
ChooseFirst
signature to work with covariant IEnumerable<out T>
interface:public abstract class FruitBox<T>
{
public T item;
public static T ChooseFirst(IEnumerable<FruitBox<T>> fruitBoxes)
{
return fruitBoxes.First().item;
}
}
Upvotes: 1
Reputation: 2162
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);
Upvotes: 0