1. 程式人生 > >C#中的事件(event)處理機制

C#中的事件(event)處理機制

當前 gree vat val calc moni return span stat

委托


  • 語法

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 }

C#中的事件(event)處理機制