Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to add an apple delegate to a list of fruit delegates?
- 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) { }
- }
- The best overloaded method match for 'List<FruitDelegate<Fruit>>.Add(FruitDelegate<Fruit>)' has some invalid arguments`
- FruitDelegate<Fruit> f = new FruitDelegate<Fruit>(EatFruit);
- f(new Apple());
- f(new Banana());
- public delegate void FruitDelegate<in T>(T o) where T : Fruit;
- FruitDelegate<Apple> f = new FruitDelegate<Fruit>(EatFruit);
- f(new Apple());
- FruitDelegate<Fruit> f = new FruitDelegate<Apple>(EatApple); // invalid
- f(new Apple());
- f(new Banana());
Add Comment
Please, Sign In to add comment