关键词搜索

源码搜索 ×
×

C# 委托 事件 匿名方法

发布2014-01-08浏览1425次

详情内容

(*) 委托 delegate

从最简单的例子开始:

  1. namespace ConsoleApplication1
  2. {
  3. class Program
  4. {
  5. // 委托其实就相当于一个类型。这里,类型的名字叫BinaryOp
  6. public delegate int BinaryOp(int x, int y);
  7. static public int Add(int x, int y)
  8. {
  9. return x + y;
  10. }
  11. static void Main()
  12. {
  13. // 创建一个BinaryOp类型的实例,用静态Add方法初始化
  14. BinaryOp d = new BinaryOp(Add);
  15. Console.WriteLine(d(10, 10));
  16. Console.ReadLine();
  17. }
  18. }
  19. }

输出结果为:20

上面是静态方法的委托,下面再来看一个实例方法的委托。
  1. class MyClass
  2. {
  3. private string name;
  4. public MyClass(string name)
  5. {
  6. this.name = name;
  7. }
  8. public void DisplayName()
  9. {
  10. Console.WriteLine("{0}", name);
  11. }
  12. }
  13. class Program
  14. {
  15. // 委托其实就相当于一个类型。这里,类型的名字叫SimpleDelegate
  16. public delegate void SimpleDelegate();
  17. static void Main()
  18. {
  19. MyClass a = new MyClass("A");
  20. MyClass b = new MyClass("B");
  21. // 用实例方法DisplayName初始化
  22. SimpleDelegate d = new SimpleDelegate(a.DisplayName);
  23. d();
  24. d = new SimpleDelegate(b.DisplayName);
  25. d();
  26. Console.ReadLine();
  27. }
  28. }

输出结果为:A

                       B  

(*) 事件
委托是个类型,而事件是个成员。看下面的代码:

  1. namespace ConsoleApplication1
  2. {
  3. public class SimpleMath
  4. {
  5. public delegate int BinaryOp(int a, int b); // 定义Binary类型
  6. public event BinaryOp BinaryEvent; // 定义BinaryEvent成员
  7. public int Add(int a, int b) { return a + b; }
  8. public int Substract(int a, int b) { return a - b; }
  9. public int Calculate()
  10. {
  11. // Raise the event by using the () operator.
  12. return BinaryEvent(1, 2); // 只能在定义事件的类的内部调用,如果写在外面会编译不过
  13. }
  14. }
  15. class Program
  16. {
  17. static void Main()
  18. {
  19. SimpleMath sm = new SimpleMath();
  20. // sm.BinaryEvent(1, 2); 编译错误!只能在定义事件的类的内部调用
  21. // 下面两种注册方法效果是一样的,相当于注册了两遍,也的确会依序执行两遍
  22. sm.BinaryEvent += new SimpleMath.BinaryOp(sm.Add);
  23. sm.BinaryEvent += sm.Add;
  24. Console.WriteLine(sm.Calculate()); // 结果是3
  25. // 下面两种注册方法效果是一样的,相当于注册了两遍,也的确会依序执行两遍
  26. sm.BinaryEvent += new SimpleMath.BinaryOp(sm.Substract);
  27. sm.BinaryEvent += sm.Substract;
  28. Console.WriteLine(sm.Calculate()); // -1, 只保留最后一次调用的返回值(3,3,-1,-1)
  29. Console.ReadLine();
  30. }
  31. }
  32. }

输出结果为:3

                      -1

(*) 匿名方法
匿名方法的作用是使代码更简化,方便了程序员。如果没有匿名方法,则定义一套事件监听需要这样:

  1. class Program
  2. {
  3. public delegate void SomeDelegate(); // 定义委托
  4. static void Main()
  5. {
  6. SomeType obj = new SomeType();
  7. obj.SomeEvent += new SomeDelegate(MyEventHandler);
  8. }
  9. // 一般来说,定义的MyEventHandler方法只用来响应事件,只在上面那一处地方使用
  10. public static void MyEventHandler() // 定义委托所调用的方法
  11. {}
  12. }
上面的代码比较啰嗦,尤其是为委托和它所调用的方法起名字比较费劲我觉得。有了匿名方法以后,仅需要这样:
  1. class Program
  2. {
  3. static void Main()
  4. {
  5. SomeType obj = new SomeType();
  6. obj.SomeEvent += delegate{
  7. // 实现事件处理逻辑
  8. }; // 注意要有分号
  9. }
  10. }
与上一段代码相比,省去了SomeDelegate和MyEventHandler的定义。

拓展: 点击打开链接

相关技术文章

最新源码

下载排行榜

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载