1.什么是委托.
MSDN上解釋..
1). 委托是一種定義方法簽名的類型。 當實例化委托時,您可以將其實例與任何具有兼容簽名的方法相關聯。 與委托的簽名(由返回類型和參數組成)匹配的任何可訪問類或結構中的任何方法都可以分配給該委托. 方法可以是靜態方法,也可以是實例方法。 這樣就可以通過變成方式來更改方法調用,還可以向現有類中插入新代碼。只要知道委托的簽名,就可以分配您自己的方法。
2). 您可以通過委托實例調用方法。事件處理程序就是通過委托調用的方法。
3). 委托用于將方法做為參數傳遞給其他方法。
4). 您可以創建一個自定義方法,當發生特定事件時某個類(例如 Windows控件) 就可以調用您的方法。
委托的特點:
1). 委托類似于C++函數指針.但他們是類型安全的。
2). 委托允許將方法作為參數進行傳遞。
3). 委托可用于定義回調方法。
4). 委托可以鏈接在一起; 例如,可以對一個事件調用多個方法。
5). 方法不必與委托簽名簽名安全匹配.
總結了下,主要分為兩類:
1). 委托可以做為方法的類型。方法可以賦值給委托的實例,且可以用“+=”賦值多個,也可以用"-="移除.
2). 可以做為回調函數。
因為我們主要講委托和事件,所以主要講類1
1.委托
//Create a delegate public delegate void Del(string message); //Create a method for a delegate. public static void delegateMethod1(string message) { System.Console.WriteLine("I am delegateMethod1:"+message); } static void Main(string[] args)
{ //Instantiate the delegate. Del handler = delegateMethod1; //call the delegate. handler("Hello World");
如上圖, 創建一個委托和一個簽名相同的方法,將方法賦值給委托的實例,傳參調用委托和調用方法一樣.
//Create a method for a delegate. public static void delegateMethod2(string message) { System.Console.WriteLine("I am delegateMethod2:"+message); } static void Main(string[] args) { //Instantiate the delegate. Del handler = delegateMethod1; //call the delegate. handler("Hello World"); Console.WriteLine(); //add second delegate handler += delegateMethod2; //call the delegate. handler("Bye World"); Console.ReadLine(); }
將另一個方法用“+=”符號,鏈接到委托實例上,當調用該委托實例時就依次調用這兩個方法.
同理可以用“-=”符號,移除委托中的方法.
//Instantiate the delegate. Del handler = delegateMethod1; //call the delegate. handler("Hello World"); Console.WriteLine(); //add second delegate handler += delegateMethod2; //call the delegate. handler("Bye World"); Console.WriteLine(); //move method handler -= delegateMethod1;
handler("Hello world"); Console.ReadLine();
移除一個方法后的執行結果
上述可以通過加減賦值的叫多播委托,也是委托的一個作用
2. 事件
MSDN上事件解釋: 類或對象可以通過事件向其他類或對象通知發生的相關事情。 發送(或引發)事件的類稱為“發行者”,接收(或處理)事件的類稱為“訂戶”。
通過解釋知道事件的主要作用是通知其他對象或類發生了事情
winform程序和web程序主要通過訂閱控件引發事件
事件的定義:
//Create a eventpublic event Del eventDel;
看看事件的定義是不是和委托有點像,很像直接實例化的委托,只不過前面多了一個event關鍵字.
事件的作用是通知其他類發生了事情,那么怎么樣通知呢?
1). 事件的通知
下面看個例程:
class MainNotify { //定義一個委托 public delegate void NotifyDelegate(); //定義一個事件 public event NotifyDelegate notifyevent; //方法 public void Notify() { if (notifyevent != null) { notifyevent(); } } }class PRogram { //Receive Notify Method public static void NotifyPocess() { System.Console.WriteLine("Receive Notify!"); }
static void Main(string[] args) { //實例化類型 MainNotify mainnotify = new MainNotify(); //事件綁定方法 mainnotify.notifyevent += NotifyPocess; //執行方法 mainnotify.Notify();
}
}
1.在MainNotify類中定義了一個事件,定義一個方法,方法執行時觸發事件
2.實例化這個類,將事件用"+="綁定這個接收方法,被綁定的方法叫訂閱這個事件,如果取消訂閱,事件觸發時就不會執行這個方法.
執行結果
看看事件的使用和委托是不是類似,事件說白了就是實例化的委托.
2). 事件也可以多播
//Receive Notify Method public static void NotifyPocess() { System.Console.WriteLine("Receive Notify!"); } //Receive notify Method public static void notifyProcess1() { System.Console.WriteLine("Receive Notify second!"); } //實例化類型 MainNotify mainnotify = new MainNotify(); //事件綁定方法 mainnotify.notifyevent += NotifyPocess; mainnotify.notifyevent += notifyProcess1; //執行方法 mainnotify.Notify();
執行結果
如果方法不是靜態方法,需要用委托實例化方法
//Receive Notify Method public void NotifyPocess() { System.Console.WriteLine("Receive Notify!"); } //Receive notify Method public void NotifyProcess1() { System.Console.WriteLine("Receive Notify second!"); } //實例化類型 MainNotify mainnotify = new MainNotify(); //事件綁定方法 mainnotify.notifyevent += new MainNotify.NotifyDelegate(NotifyPocess); mainnotify.notifyevent += new MainNotify.NotifyDelegate(NotifyProcess1); //執行方法 mainnotify.Notify();
winform控件事件程序
在窗體上添加一個按鈕,雙擊進入方法,添加事件處理程序,點擊按鈕,事件處理程序觸發.
下面演示怎么通過按鈕事件觸發程序
1.添加方法
private void button1_Click(object sender, EventArgs e) { System.Console.WriteLine("Receive Button Event!"); }
2.按鈕自帶委托和事件
// 摘要: // 表示將處理不包含事件數據的事件的方法。 // // 參數: // sender: // 事件源。 // // e: // 不包含任何事件數據的 System.EventArgs。 [Serializable] [ComVisible(true)] public delegate void EventHandler(object sender, EventArgs e); public event EventHandler Click;
3. 方法注冊事件
this.button1.Click += new System.EventHandler(this.button1_Click);
4.點擊按鈕事件觸發通知方法
新聞熱點
疑難解答