網站首頁 編程語言 正文
多播委托
簡介
- 每一個委托都是繼承自MulticastDelegate,也就是每個都是多播委托。
- 帶返回值的多播委托只返回最后一個方法的值
- 多播委托可以用加減號來操作方法的增加或者減少。
- 給委托傳遞相同方法時 生成的委托實例也是相同的(也就是同一個委托)
代碼實現
//聲明委托
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; //給委托傳遞相同方法時 生成的委托實例也是相同的(也就是同一個委托)
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
}
泛型委托
代碼實現
//泛型委托聲明
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
}
原文鏈接:https://www.cnblogs.com/wml-it/p/16070173.html
相關推薦
- 2022-08-07 C++文件的數據寫入和文件的數據讀取的方法實現_C 語言
- 2022-06-29 Tomcat中使用ipv6地址的示例代碼_Tomcat
- 2022-11-09 PostgreSQL索引掃描時為什么index?only?scan不返回ctid_PostgreSQ
- 2022-11-09 Android性能優化之plt?hook與native線程監控詳解_Android
- 2022-06-29 Oracle執行Update語句的幾種方式_oracle
- 2022-09-19 C語言數據結構之單鏈表存儲詳解_C 語言
- 2022-10-16 Python?讀取?Word?文檔操作_python
- 2022-05-21 React?組件中的state和setState()你知道多少_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支