C#多個方法的關聯概述
作者:佚名
這里介紹C#多個方法的關聯就和命名方法一樣,將多個匿名方法與同一個委托進行關聯是可能的。這在很多情況下會非常有用——首先想到的是把一個簡單的處理程序添加給按鈕的點擊事件。
C#語言還是比較常見的東西,這里我們主要介紹C#多個方法的關聯,包括介紹相關聯一個匿名方法和一個命名方法等方面。
C#多個方法的關聯
C#多個方法的關聯就和命名方法一樣,將多個匿名方法與同一個委托進行關聯是可能的。這在很多情況下會非常有用——首先想到的是把一個簡單的處理程序添加給按鈕的點擊事件。下面的代碼顯示了一個委托,它同時帶有與之相關聯一個匿名方法和一個命名方法:
- privatedelegatevoidExample4(stringfirstName,stringlastName);
- privatevoidbtnExample4_Click(objectsender,EventArgse)
- {
- //Setupourparameters.
- stringparameter1="Zach";
- stringparameter2="Smith";
- //CreateaninstanceoftheExample4delegatewithan
- //anonymousmethod.
- Example4example=
- newExample4(
- delegate(stringfirstName,stringlastName)
- {
- MessageBox.Show("Example4:"+firstName+""+lastName);
- });
- //Addanothermethodtothedelegate-thistime
- //anamedmethod.
- example+=newExample4(Example4NamedMethod);
- //Executethedelegate.
- example(parameter1,parameter2);
- }
- privatevoidExample4NamedMethod(stringfirstName,stringlastName)
- {
- MessageBox.Show("Example4Method:"+firstName+""+lastName);
- }
【編輯推薦】
責任編輯:佚名
來源:
cnblogs