委托——delegate

作者:追风剑情 发布于:2015-9-30 10:51 分类:C#

示例一:

利用委托,可使方法做为参数传递。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace CSharpTest
  7. {
  8. class Program
  9. {
  10. //利用委托,可使方法做为参数传递。
  11. public delegate int PerformCalculation(int x, int y);
  12.  
  13. static void Main(string[] args)
  14. {
  15. Print( Calculation );
  16.  
  17. Console.Read();
  18. }
  19.  
  20. public static int Calculation(int x, int y)
  21. {
  22. return x + y;
  23. }
  24.  
  25. public static void Print(PerformCalculation handler)
  26. {
  27. int val = handler(20, 30);
  28. Console.WriteLine(val);
  29. }
  30. }
  31. }

运行效果

wwwww.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号