C#委托使用:多播 ,向委托注册多个方法

           private static void EnglishGreeting(string name) {
               Console.WriteLine("Morning, " + name);
           }

           private static void ChineseGreeting(string name) {
               Console.WriteLine("早上好, " + name);
           }

   

           static void Main(string[] args) {
       
               GreetingDelegate gd = EnglishGreeting;
               gd += ChineseGreeting;
               gd("多播调用");

               Console.ReadKey();
           }

  

由于委托类型派生自 System.Delegate,因此可以在委托上调用该类定义的方法和属性。  例如,若要查询委托调用列表中方法的数量,你可以编写: 

   int invCount= gd.GetInvocationList().Count();

  

移除指定绑定方法-=

   GreetingDelegate gd = EnglishGreeting;
               gd -= EnglishGreeting;

               gd += ChineseGreeting;

               gd("多播调用");
               int invCount= gd.GetInvocationList().Count();
               Console.WriteLine(invCount);
               Console.ReadKey();

  

如果移除已经移除的绑定方法 那么也不会报错

原文地址:https://www.cnblogs.com/ProDoctor/p/6068183.html