Program.cs 767 B

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Remoting.Messaging;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace ConsoleDelegate
  8. {
  9. class Test
  10. {
  11. public int Add(int x, int y)
  12. {
  13. return x + y;
  14. }
  15. }
  16. internal class Program
  17. {
  18. //定义委托
  19. public delegate int DAdd(int x, int y);
  20. static void Main(string[] args)
  21. {
  22. Test test = new Test();
  23. //将委托与Add方法相关联
  24. DAdd d = test.Add;
  25. //再想调用Test类中的Add方法时,直接调用委托就可以了
  26. int num = d(2,3);
  27. Console.WriteLine(num);
  28. Console.ReadLine();
  29. }
  30. }
  31. }