C#中的多播委托和泛型委托
更新時(shí)間:2022年05月04日 16:25:59 作者:農(nóng)碼一生
這篇文章介紹了C#中的多播委托和泛型委托,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
多播委托
簡(jiǎn)介
- 每一個(gè)委托都是繼承自MulticastDelegate,也就是每個(gè)都是多播委托。
- 帶返回值的多播委托只返回最后一個(gè)方法的值
- 多播委托可以用加減號(hào)來(lái)操作方法的增加或者減少。
- 給委托傳遞相同方法時(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#把UNICODE編碼轉(zhuǎn)換為GB編碼的實(shí)例
下面小編就為大家?guī)?lái)一篇C#把UNICODE編碼轉(zhuǎn)換為GB編碼的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
C#保存listbox中數(shù)據(jù)到文本文件的方法
這篇文章主要介紹了C#保存listbox中數(shù)據(jù)到文本文件的方法,涉及C#操作listbox數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2015-04-04
淺析C#?AsyncLocal如何實(shí)現(xiàn)Thread間傳值
這篇文章主要是來(lái)和大家一起討論一下C#?AsyncLocal如何實(shí)現(xiàn)Thread間傳值,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
通過(guò)?C#/VB.NET?代碼將?Excel?工作表拆分為單獨(dú)的文件
這篇文章主要介紹了通過(guò)C#/VB.NET代碼將Excel工作表拆分為單獨(dú)的文件,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
C#實(shí)現(xiàn)簡(jiǎn)單的天氣預(yù)報(bào)示例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)單的天氣預(yù)報(bào)示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

