C#中的多播委托和泛型委托

作者:農碼一生 时间:2022-03-23 19:17:28 

多播委托

简介

  • 每一个委托都是继承自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

标签:C#,多播,泛型,委托
0
投稿

猜你喜欢

  • Java实现批量向mysql写入数据的方法

    2023-11-05 20:51:21
  • SpringCloud Feign配置应用详细介绍

    2023-07-14 04:23:03
  • Maven 多profile及指定编译问题的解决

    2022-04-22 23:43:24
  • 浅谈一下Java中的悲观锁和乐观锁

    2023-08-12 05:54:27
  • 合成聚合复用原则_动力节点Java学院整理

    2023-06-27 22:05:20
  • Java 常量池详解之字符串常量池实现代码

    2022-09-09 22:12:03
  • TextView实现跑马灯效果 就这么简单!

    2023-06-25 18:42:24
  • 关于RedisTemplate之opsForValue的使用说明

    2023-07-09 16:53:04
  • 基于Idea+Jconsole实现线程监控步骤

    2021-07-29 10:39:40
  • java arrayList遍历的四种方法及Java中ArrayList类的用法

    2023-11-17 17:49:55
  • 详解maven中profiles使用实现

    2022-11-13 23:14:24
  • Java中ArrayList初始化的四种方法详解

    2022-03-29 21:50:13
  • Spring MVC项目中的异常处理详解

    2021-12-22 09:19:20
  • java中匿名内部类详解

    2022-10-06 14:56:56
  • Java 深入探讨设计模式之原型模式篇

    2023-11-16 17:37:59
  • Java 数据结构之堆的概念与应用

    2023-11-24 05:36:18
  • java中获取json的所有key方法

    2023-10-15 06:15:26
  • JavaWeb项目部署到服务器详细步骤详解

    2023-11-29 11:15:20
  • SpringBoot中Dozer的使用小结

    2023-11-25 01:24:38
  • java开发线上事故理解RocketMQ异步精髓

    2023-07-25 07:41:20
  • asp之家 软件编程 m.aspxhome.com