I have a sample program with a base Fruit
class and a derived Apple
class.
class Testy
{
public delegate void FruitDelegate<T>(T o) where T : Fruit;
private List<FruitDelegate<Fruit>> fruits = new List<FruitDelegate<Fruit>>();
public void Test()
{
FruitDelegate<Apple> f = new FruitDelegate<Apple>(EatFruit);
fruits.Add(f); // Error on this line
}
public void EatFruit(Fruit apple) { }
}
I want to have a list of fruit delegates and be able to add delegates of more derived fruit to the list. I believe this has something to do with covariance or contravariance but I cannot seem to figure it out.
The error message is (without namespaces):
The best overloaded method match for 'List<FruitDelegate<Fruit>>.Add(FruitDelegate<Fruit>)' has some invalid arguments`