委托与匿名方法

作者:追风剑情 发布于:2019-7-18 16:14 分类:C#

示例

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Test
  8. {
  9. delegate void Printer(string s);
  10.  
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. // 使委托与匿名方法关联
  16. Printer p = delegate (string j)
  17. {
  18. System.Console.WriteLine(j);
  19. };
  20. p("The delegate using the anonymous method is called.");
  21.  
  22. // 使委托与命名方法 (DoWork) 关联
  23. p = new Printer(DoWork);
  24. p("The delegate using the named method is called.");
  25.  
  26. Console.ReadLine();
  27. }
  28.  
  29. static void DoWork(string k)
  30. {
  31. System.Console.WriteLine(k);
  32. }
  33. }
  34. }

运行测试

1111.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号