委托与事件

委托的作用

解耦

委托声明

无返回值无参数列表 public delegate  void Mydelegate ()

无返回值有参数列表 public delegate  void Mydelegate (int count)

有返回值有参数列表 public delegate  int Mydelegate (int count)

创建一个方法应该与声明的委托返回值参数列表一样。

 用没有返回值的 Mydelegate mydele = new  Mydelegate(this.ListCount);

        mydele.invoke(参数);

多播委托,会按方法列表执行

Mydelegate mydele = new  Mydelegate(this.ListCount);

      mydele+=new  Mydelegate(MyClass.ListCount);

也可以相减。

事件是委托的一种实例。委托是一个类型。

事件声明 public  event Mydelgate;

但是事件不可以在传递方法的时候调用invoke  需要封装一下。

原文地址:https://www.cnblogs.com/LahGo/p/6928810.html