C#中的事件(event)处理机制

委托


  • 语法

1 [访问修饰符] delegate 返回类型 委托名();
  • 委托的特点

  类似于C++函数指针,但它是类型安全的;委托允许将方法作为参数进行传递;委托可用于定义回调方法;委托可以链接在一起:如,可以对一个事件调用多个方法。

  委托在运行时确定调用哪种方法,且委托和方法必须具有相同的签名。

 1 //定义委托
 2 public delegate int Callback(int num1, int num2);
 3 //定义方法类
 4 class MathCalc{
 5     //加法方法
 6     public int Plus(int number1, int number2){
 7         return number1 + number2;
 8     }
 9 }
10 
11 static void Main(string[] args){
12     //委托的对象
13     Callback objCall;
14     //实例MathCalc类的对象
15     MathCalc mc = new MathCalc();
16     //将方法与委托关联起来
17     objCall = new Callback(mc.Plus);
18     //可以用Callback objCall = new Callback(mc.Plus);声明委托并与方法关联
19     //将委托实例化
20     int result = objCall(1, 2);
21     System.Console.WriteLine("结果为{0}", result);
22 }

匿名方法


 

  委托能正常调用,须创建单独的方法,增加了实例化委托所需的编码系统开销。为此可以使用匿名方法,其不需要定义委托要引用的方法,而把要引用的方法的方法体作为实例化委托时的一个代码块紧跟在委托后面。

  • 语法
1 委托类型 委托实例 = delegate([参数列表]) { 
2   //代码块 
3 }; //分号不能少
  • 使用方法
 1 class Program{
 2     //定义委托
 3     delegate int delSum(int num1, int num2);
 4     static void Main(string[] args){
 5         int third = 3;
 6         //将代码传递为委托参数
 7         delSum deladd = delegate(int first, int second){
 8             int sum = first + second;
 9             return sum;
10         };
11         int total = deladd(1, 2) + third;
12         Console.WriteLine("1+2+3="+total);
13     }
14 }
  • 匿名方法的特点

  匿名方法的优点是减少了系统的开销,方法仅在由委托使用时才定义。匿名方法有助于降低代码的复杂性,但如果委托代码块的代码很多则容易混淆代码,降低程序的可读性。

  在匿名方法中不能使用跳转语句跳转到匿名方法的外部,同样匿名方法外部的跳转语句也不能跳转到匿名方法的内部。匿名方法不能访问在外部使用ref和out修饰符的参数,但可以访问在外部声明的其他变量。

事件


 

  事件是一种特殊的委托,在某件事情发生时,一个对象可以通过事件通知另一个对象。

  1. 定义事件
  2. 为对象订阅该事件
  3. 将发生的事件通知给订阅人
 1 public class MeltdownEventArgs : EventArgs {
 2     private string message;
 3     public MeltdownEventArgs(string message) {
 4         this.message = message;
 5     }
 6     public string Message {
 7         get {
 8             return message;
 9         }
10     }
11 }
12 
13 public class Reactor {
14     private int temperature;
15     //定义委托
16     public delegate void MeltdownHandler(object reactor, MeltdownEventArgs MEA);
17     //定义事件
18     public event MeltdownHandler OnMeltdown;
19     public int Temperature {
20         set {
21             temperature = value;
22             //当温度值高于1000时,创建叫myMEA的MeltdownEventArgs对象,并把字符串传递进来给myMEA的message属性,然后产生OnMeltdown事件,把当前的Reactor对象和myMEA作为参数传递给OnMeltdown
23             if (temperature > 1000){
24                 MeltdownEventArgs myMEA = new MeltdownEventArgs("Reactor meltdown in progress!");
25                 OnMeltdown(this, myMEA);
26             }
27         }
28     }
29 }
30 
31 public class ReactorMonitor {
32     //ReactorMonitor构造函数接收Reactor对象myReactor为参数
33     public ReactorMonitor(Reactor myReactor) {
34         //该构造函数为OnMeltdown事件监视myReactor对象。当myReactor对象产生OnMeltdown事件时,DisplayMessage()方法会被调用来处理这个事件
35         myReactor.OnMeltdown += new Reactor.MeltdownHandler(DisplayMessage);
36     }
37     public void DisplayMessage(object myReactor, MeltdownEventArgs myMEA) {
38         Console.WriteLine(myMEA.Message);
39     }
40 }
41 
42 public static void Main(string[] args) {
43     Reactor myReactor = new Reactor();
44     ReactorMonitor myReactorMonitor = new ReactorMonitor(myReactor);
45 
46     Console.WriteLine("Setting reactor temperature to 100 degrees Centigrade");
47     //设置温度值为100
48     myReactor.Temperature = 100;
49     Console.WriteLine("Setting reactor temperature to 2000 degrees Centigrade");
50     //设置温度值为2000
51     myReactor.Temperature = 2000;
52 }

猜你喜欢

转载自www.cnblogs.com/goalie/p/9463901.html