根据上篇博文的反响我想向大家说明,因为本人时间有限加上才疏学浅,无法写出面面俱到的博文。我只是把自己经历的觉得有些意义的技术点分享出来,希望大家见谅。当然,我不会让大家走上歧途的。所有示例都是亲测。 今天我想讨论一下事件与委托之间的联系。很多人说,事件即委托。真的是这样吗?那么微软为何又要定义事件与委托两个概念呢? 首先我们来看一下事件的定义以及调用:
//定义委托 public delegate int MathHandle(int a, int b); public class Test { //定义事件 public event MathHandle MathEvent; public int Add(int a, int b) { return a + b; } public void Operation(int a, int b) { Console.WriteLine(this.MathEvent(a, b)); } } [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { Test t = new Test(); //为事件方法队列添加方法 t.MathEvent += new MathHandle(t.Add); t.Operation(1,2);//此处如果调用t.MathEvent(1,2)会编译错误; } } 执行结果: 我们再看看,如果我们将使用委托来实现上述方法: //定义委托 public delegate int MathHandle(int a, int b); public class Test { public MathHandle MathEvent; public int Add(int a, int b) { return a + b; } } [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { Test t = new Test(); //为委托队列添加方法 t.MathEvent += new MathHandle(t.Add); Console.WriteLine(t.MathEvent(1, 2));//委托可以直接在实例外部执行。 } } 执行结果: 从上面示例我们看到,同一个方法你可以使用委托来调用,你也可以使用事件来调用。那究竟有何不同? 希望能从这个小小的示例,向大家传达到事件与委托的区别。 public class Heater { private int temperature; public void BoilWater() { for (int i = 1; i <= 100; i++) { temperature = i; if (temperature > 95 && BoilEvent != null) { //执行委托,无需知道要执行哪些方法 BoilEvent(temperature); } } } public delegate void BoilHandle(int param); public event BoilHandle BoilEvent;//封装了委托 } public class Alarm { public void MakeAlert(int param) { Console.WriteLine("Alarm:水温已经超过{0}度.",param); } } public class Display { public void ShowMsg(int param) { Console.WriteLine("Display:水已烧开,当前温度{0}度.", param); } } [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { Heater heater = new Heater(); Alarm alarm = new Alarm(); heater.BoilEvent += alarm.MakeAlert; heater.BoilEvent += new Display().ShowMsg; heater.BoilWater(); } }
更多WEB开发技术请加群:Asp.Net高级群 号码:261882616 博主以及同事和你共同探讨感兴趣的话题。 原文:http://www.cnblogs.com/lilin123/archive/2012/12/20/2826514.html
|
|