public class Publisher { // Declare the delegate (if using non-generic pattern). public delegate void SampleEventHandler(object sender, SampleEventArgs e); // Declare the event. public event SampleEventHandler SampleEvent; // Wrap the event in a protected virtual method // to enable derived classes to raise the event. protected virtual void RaiseSampleEvent() { // Raise the event by using the () operator. SampleEvent(this, new SampleEventArgs("Hello")); } }
C# 中的委托和事件using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace ConsoleApplication3 { class Program { public static void Main() { ShowTest st = new ShowTest(); AddShow sh = new AddShow(st); LessShow ls = new LessShow(st); st.ActivateShowTest(7, 2); } } //参数类 class ShowEventArgs : EventArgs { public int i; public int j; public ShowEventArgs(int i, int j) { this.i = i; this.j = j; }
} //调用方法 class ShowTest { public delegate void ShowEventHandler(object sender, ShowEventArgs e); public event ShowEventHandler ShowEvent; public void ActivateShowTest(int i, int j) { ShowEventArgs ShowArgs = new ShowEventArgs(i, j); ShowEvent(this, ShowArgs); } } //加 class AddShow { public AddShow(ShowTest ShowTest) { ShowTest.ShowEvent += new ShowTest.ShowEventHandler(add); } void add(object sender, ShowEventArgs e) { Console.WriteLine("{0}+{1}={2}", e.i, e.j, e.i + e.j); } } //减 class LessShow { public LessShow(ShowTest ShowTest) { ShowTest.ShowEvent += new ShowTest.ShowEventHandler(less); } void less(object sender, ShowEventArgs e) { Console.WriteLine("{0}-{1}={2}", e.i, e.j, e.i - e.j); } } }
{
// Declare the delegate (if using non-generic pattern).
public delegate void SampleEventHandler(object sender, SampleEventArgs e); // Declare the event.
public event SampleEventHandler SampleEvent; // Wrap the event in a protected virtual method
// to enable derived classes to raise the event.
protected virtual void RaiseSampleEvent()
{
// Raise the event by using the () operator.
SampleEvent(this, new SampleEventArgs("Hello"));
}
}
白话说: 当一个"事件"出现 会依此通知"事件"的订阅者 那就是所谓的"委托"了 "委托"相当于一个函数指针,当调用它的时候将调用到它对应的函数体. "事件"订阅者不一定只有1个 或许没有 或许有很多. 这些订阅者都是"委托"类型的.要在"事件"上增加订阅者 就是咱们常见的 对象."事件" += new "委托"()只有对应的"委托"可以成为"事件"的订阅者 是有规范的.比如"上级"下达了一个"命令" 要求"各地财政厅" 完成 "某项工作" 那么 只有"财政厅"类型的政府部门 才可以接上级下达的命令.
delegate声明的变量编绎器 自动将其它继承MulticastDelegate(多播委托),也就是说委托变量,可以绑定到多个函数指针(方法)。GetInvocationList().Length可以知道这个委托变量绑定的方法个数。
这点从使用的角度看与事件一样。事件也可以将其绑定到多个方法签名。个人认为,事件与委托的区别,应该从面向对象设计的角度去,某类应该将其事件暴露给Client程序,而不应将该类的pubilc委托变量暴露给Client, 某类发生某事,触发另外某事,更符合人们的理解,更能模拟现实。
事件的底层的实现还是委托,可以认为事件是委托更高一层次的抽象,抽象的结果给我们带来更多的处理灵活性。
以上个人意见,错误之处,肯请拍砖。
事件更不是委托的集合
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication3
{
class Program
{
public static void Main()
{
ShowTest st = new ShowTest();
AddShow sh = new AddShow(st);
LessShow ls = new LessShow(st); st.ActivateShowTest(7, 2);
}
}
//参数类
class ShowEventArgs : EventArgs
{
public int i;
public int j; public ShowEventArgs(int i, int j)
{
this.i = i;
this.j = j;
}
} //调用方法
class ShowTest
{
public delegate void ShowEventHandler(object sender, ShowEventArgs e);
public event ShowEventHandler ShowEvent; public void ActivateShowTest(int i, int j)
{
ShowEventArgs ShowArgs = new ShowEventArgs(i, j);
ShowEvent(this, ShowArgs); }
} //加
class AddShow
{
public AddShow(ShowTest ShowTest)
{
ShowTest.ShowEvent += new ShowTest.ShowEventHandler(add);
}
void add(object sender, ShowEventArgs e)
{
Console.WriteLine("{0}+{1}={2}", e.i, e.j, e.i + e.j);
}
} //减
class LessShow
{
public LessShow(ShowTest ShowTest)
{
ShowTest.ShowEvent += new ShowTest.ShowEventHandler(less);
}
void less(object sender, ShowEventArgs e)
{
Console.WriteLine("{0}-{1}={2}", e.i, e.j, e.i - e.j);
}
}
}