網(wǎng)站首頁 編程語言 正文
多播委托
簡介
- 每一個委托都是繼承自MulticastDelegate,也就是每個都是多播委托。
- 帶返回值的多播委托只返回最后一個方法的值
- 多播委托可以用加減號來操作方法的增加或者減少。
- 給委托傳遞相同方法時 生成的委托實例也是相同的(也就是同一個委托)
代碼實現(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; //給委托傳遞相同方法時 生成的委托實例也是相同的(也就是同一個委托)
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
}
泛型委托
代碼實現(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
}
原文鏈接:https://www.cnblogs.com/wml-it/p/16070173.html
相關(guān)推薦
- 2022-07-21 react中的redux
- 2022-09-22 線程池的拒絕策略
- 2023-02-25 C++中vector迭代器失效與深淺拷貝問題詳析_C 語言
- 2022-05-26 Pytorch中使用ImageFolder讀取數(shù)據(jù)集時忽略特定文件_python
- 2023-11-26 StringBuffer 和 StringBuilder
- 2022-07-02 教你使用Pycharm配置遠程Jupyter_python
- 2022-05-17 Spring Cloud Loadbalancer 修改默認緩存為Caffeine,修改微服務啟動關(guān)
- 2022-08-29 Python可視化神器pyecharts之繪制箱形圖_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支