Guest User

Untitled

a guest
Aug 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. How to add an apple delegate to a list of fruit delegates?
  2. class Testy
  3. {
  4. public delegate void FruitDelegate<T>(T o) where T : Fruit;
  5.  
  6. private List<FruitDelegate<Fruit>> fruits = new List<FruitDelegate<Fruit>>();
  7.  
  8. public void Test()
  9. {
  10. FruitDelegate<Apple> f = new FruitDelegate<Apple>(EatFruit);
  11.  
  12. fruits.Add(f); // Error on this line
  13. }
  14.  
  15. public void EatFruit(Fruit apple) { }
  16. }
  17.  
  18. The best overloaded method match for 'List<FruitDelegate<Fruit>>.Add(FruitDelegate<Fruit>)' has some invalid arguments`
  19.  
  20. FruitDelegate<Fruit> f = new FruitDelegate<Fruit>(EatFruit);
  21. f(new Apple());
  22. f(new Banana());
  23.  
  24. public delegate void FruitDelegate<in T>(T o) where T : Fruit;
  25.  
  26. FruitDelegate<Apple> f = new FruitDelegate<Fruit>(EatFruit);
  27. f(new Apple());
  28.  
  29. FruitDelegate<Fruit> f = new FruitDelegate<Apple>(EatApple); // invalid
  30. f(new Apple());
  31. f(new Banana());
Add Comment
Please, Sign In to add comment