c#事件和委托
一、委托(Delegate)
1、定義
delegate是C#中的一種類型,它實際上是一個能夠持有對某個方法的引用的類。與其它的類不同,delegate類能夠
擁有一個簽名(signature),并且它"只能持有與它的簽名相匹配的方法的引用"。它所實現的功能與C/C++中的函數指針
十分相似。它允許你傳遞一個類A的方法m給另一個類B的對象,使得類B的對象能夠調用這個方法m。但與函數指針相比,
delegate有許多函數委托和事件在 .Net Framework中的應用非常廣泛指針不具備的優點。首先,函數指針只能指向靜態
函數,而delegate既可以引用靜態函數,又可以引用非靜態成員函數。在引用非靜態成員函數時,delegate不但保存了對
此函數入口指針的引用,而且還保存了調用此函數的類實例的引用。其次,與函數指針相比,delegate是面向對象、類型
安全、可靠的受控(managed)對象。也就是說,runtime能夠保證delegate指向一個有效的方法,你無須擔心delegate會
指向無效地址或者越界地址。
2、創建步驟
(1)、聲明一個delegate對象,它應當與你想要傳遞的方法具有相同的參數和返回值類型。
(2)、創建delegate對象,并"將你想要傳遞的函數作為參數傳入"。
(3)、在要實現異步調用的地方,通過上一步創建的對象來調用方法。
3、代碼實現
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
namespace?DelegateEventApp
{
????class?DelegateUtilClass
????{
????????static?void?Main(string[]?args)
????????{
????????????Console.WriteLine("輸出結果是:");
????????????DelegateUtil?du?=?new?DelegateUtil(DelegateUtilClass.OutPut);?//?步驟2,創建DelegateUtil對象
????????????du("I'm?learning?delegate?now?!");//?步驟3,調用DelegateUtil
????????????Console.ReadLine();
????????}
????????public?delegate?void?DelegateUtil(string?sName);?//?步驟1,聲明delegate對象z
????????///?<summary>
????????///?描述:這是想要傳遞的方法,該方法與定義的委托(即DelegateUtil)具有相同的參數和返回值類型,函數名是不一樣的哦!
????????///?</summary>
????????///?<param?name="sName"></param>
????????public?static?void?OutPut(string?sName)
????????{
????????????Console.WriteLine("Hi,?"?+?sName);
????????}
????}
}
?
4、輸出結果
?
二、事件(Event)
1、事件定義
C#中的事件處理實際上是一種具有特殊簽名的delegate,象下面這個樣子:public delegate void MyEventHandler
(object sender, MyEventArgs e);
其中的兩個參數,sender代表事件發送者,e是事件參數類。MyEventArgs類用來包含與事件相關的數據,所有的事件參數
類都必須從System.EventArgs類派生。當然,如果你的事件不含參數,那么可以直接用System.EventArgs類作為參數。
2、創建步驟
(1)、定義delegate對象類型,它有兩個參數,第一個參數是事件發送者對象,第二個參數是事件參數類對象。
(2)、定義事件參數類,此類應當從System.EventArgs類派生。如果事件不帶參數,這一步可以省略。
(3)、定義"事件處理方法,它應當與delegate對象具有相同的參數和返回值類型"。
(4)、用event關鍵字定義事件對象,它同時也是一個delegate對象。
(5)、用+=操作符添加事件到事件隊列中(-=操作符能夠將事件從隊列中刪除)。
(6)、在需要觸發事件的地方用調用delegate的方式寫事件觸發方法。一般來說,此方法應為protected訪問限制,既
不能以public方式調用,但可以被子類繼承。名字是OnEventName。
(7)、在適當的地方調用事件觸發方法觸發事件。
3、代碼實現
?
?1?using?System;?2?using?System.Collections.Generic;
?3?using?System.Linq;
?4?using?System.Text;
?5?
?6?namespace?DelegateEventApp
?7?{
?8?????class?EventUtilClass
?9?????{
10?????????public?delegate?void?EventUtilEventHandler(object?sender,?System.EventArgs?e);//?步驟1?定義delegate對象
11?
12?????????//?步驟2(定義事件參數類)省略
13?
14?????????public?class?EventClass
15?????????{
16?????????????public?void?EventUtil_Function(object?sender,?System.EventArgs?e)
17?????????????{
18?????????????????Console.WriteLine("Hi,?This?is?using?event?to?print?!");??//?步驟3,定義事件處理方法,它與delegate對象具有相同的參數和返回值類型
19?????????????}
20?????????}
21?
22?????????//?步驟4,用event關鍵字定義事件對象
23?????????private?event?EventUtilEventHandler?utilEvent;
24?????????private?EventClass?eventClass;
25?
26?????????//構造函數
27?????????public?EventUtilClass()
28?????????{
29?????????????eventClass?=?new?EventClass();
30?????????????this.utilEvent?+=?new?EventUtilEventHandler(eventClass.EventUtil_Function);?////?步驟5,用+=操作符將事件添加到隊列中
31?????????}
32?
33?????????//?步驟6,以調用delegate的方式寫事件觸發函數
34?????????protected?void?OnUtilEvent(System.EventArgs?e)
35?????????{
36?????????????if?(utilEvent?!=?null)
37?????????????{
38?????????????????utilEvent(this,?e);
39?????????????}
40?????????}
41?
42?????????public?void?RaiseEvent()
43?????????{
44?????????????EventArgs?e?=?new?EventArgs();
45?????????????this.OnUtilEvent(e);?//?步驟7,觸發事件
46?????????}
47?
48?????????static?void?Main(string[]?args)
49?????????{
50?????????????Console.WriteLine("輸出結果是:");
51?
52?????????????EventUtilClass?eventUtilClass?=?new?EventUtilClass();
53?????????????Console.WriteLine("請輸入一個字母\"A\"");
54?????????????string?sRead?=?Console.ReadLine();
55?????????????if?(sRead?==?"A")
56?????????????{
57?????????????????eventUtilClass.RaiseEvent();
58?????????????}
59?????????????else
60?????????????{
61?????????????????Console.WriteLine("輸入有錯誤,未觸發事件!");
62?????????????}
63?????????????Console.ReadLine();
64?????????}
65?????}
66?}
67?
?
4、輸出結果
總結
- 上一篇: Spark提交 指定 kerberos
- 下一篇: 痛!做C#半年,挣的不如做AI1个月?”