C#中的多播委托和泛型委托
多播委托
簡(jiǎn)介
- 每一個(gè)委托都是繼承自MulticastDelegate,也就是每個(gè)都是多播委托。
- 帶返回值的多播委托只返回最后一個(gè)方法的值
- 多播委托可以用加減號(hào)來操作方法的增加或者減少。
- 給委托傳遞相同方法時(shí) 生成的委托實(shí)例也是相同的(也就是同一個(gè)委托)
代碼實(shí)現(xiàn)
//聲明委托 delegate void MulticastTest(); public class MulticastDelegateTest { public void Show() { MulticastTest multicastTest = new MulticastTest(MethodTest); multicastTest(); Action action =new Action(MethodTest); action = (Action)MulticastDelegate.Combine(action, new Action(MethodTest2)); action = (Action)MulticastDelegate.Combine(action, new Action(MethodTest3)); action = (Action)MulticastDelegate.Remove(action, new Action(MethodTest3)); action(); //等同于上面 action = MethodTest; action += MethodTest2; action += MethodTest3; action -= MethodTest3; foreach (Action action1 in action.GetInvocationList()) { action1(); } Console.WriteLine("=========="); action(); Func<string> func = () => { return "我是Lambda"; }; func += () => { return "我是func1"; }; func += () => { return "我是func2"; }; func += GetTest; func += GetTest; //給委托傳遞相同方法時(shí) 生成的委托實(shí)例也是相同的(也就是同一個(gè)委托) string result = func(); Console.WriteLine(result); Console.WriteLine("=========="); } #region 委托方法 public void MethodTest() { Console.WriteLine("我是方法MethodTest()1"); } public void MethodTest2() { Console.WriteLine("我是方法MethodTest()2"); } public void MethodTest3() { Console.WriteLine("我是方法MethodTest()3"); } public string GetTest() { return "我是方法GetTest()"; } #endregion }
泛型委托
代碼實(shí)現(xiàn)
//泛型委托聲明 delegate void GenericDelegate<T>(T t); public class GenericDelegate { public static void InvokeDelegate() { GenericDelegate<string> genericDelegate = new GenericDelegate<string>(Method1); genericDelegate("我是泛型委托1"); //官方版本(不帶返回值) Action<string> action = new Action<string>(Method1); action("我是泛型委托1"); //Action<string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string> GenericDelegate<int> genericDelegate1 = new GenericDelegate<int>(Method2); genericDelegate1(2); //官方版本(帶回值) Func<string, string> func = new Func<string, string>(Method3); string ret = func("我是帶返回值Func委托"); Console.WriteLine( ret ); //Func<string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string,string> } #region 委托方法 public static void Method1(string str) { Console.WriteLine(str); } public static void Method2(int num) { Console.WriteLine("我是泛型委托2 "+num); } public static string Method3(string str ) { return str; } #endregion }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# 函數(shù)覆蓋總結(jié)學(xué)習(xí)(推薦)
下面小編就為大家?guī)硪黄狢# 函數(shù)覆蓋總結(jié)學(xué)習(xí)(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05C#如何控制IIS動(dòng)態(tài)添加刪除網(wǎng)站詳解
這篇文章主要給大家介紹了關(guān)于C#如何控制IIS動(dòng)態(tài)添加刪除網(wǎng)站的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11C#與SQL連接:GridView控件對(duì)數(shù)據(jù)庫(kù)的操作
GridView控件操作方面的知識(shí),需要的朋友可以參考一下2013-02-02C#實(shí)現(xiàn)從windows剪貼板獲取內(nèi)容的方法
這篇文章主要介紹了C#實(shí)現(xiàn)從windows剪貼板獲取內(nèi)容的方法,涉及C#操作剪貼板的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-05-05基于Avalonia實(shí)現(xiàn)自定義彈窗的示例詳解
對(duì)于使用avalonia的時(shí)候某些功能需要到一些提示,比如異常或者成功都需要對(duì)用戶進(jìn)行提示,所以需要單獨(dú)實(shí)現(xiàn)彈窗功能,并且可以自定義內(nèi)部組件,這一期將手動(dòng)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的小彈窗,并且很容易自定義,希望大家喜歡2023-02-02Unity3D基于陀螺儀實(shí)現(xiàn)VR相機(jī)功能
這篇文章主要為大家詳細(xì)介紹了Unity3D基于陀螺儀實(shí)現(xiàn)VR相機(jī)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04C#實(shí)現(xiàn)會(huì)移動(dòng)的文字效果
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)會(huì)移動(dòng)的文字效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04