C# delegate

main program


   public delegate void MyNotify(string msg);
  static void Hello(string msg) => Console.WriteLine("Hello: " + msg);
  static void Goodbye(string msg) => Console.WriteLine("Goodbye: " + msg);

  public static void test()
  {
      Console.WriteLine("\nDelegate");

      MyNotify? notifier = Hello;
      notifier.Invoke("test");
      notifier = new MyNotify(Hello); // Création d'un délégué

      notifier("Titi");
      notifier += Goodbye; // Ajout d'une deuxième méthode
      notifier("Jacques"); // Appelle Hello puis Goodbye
      notifier -= Goodbye;
      if (notifier != null)
          notifier("Bunny");

      Console.WriteLine(notifier?.GetInvocationList().Length);
  }
 

console

Delegate Hello: test Hello: Titi Hello: Jacques Goodbye: Jacques Hello: Bunny 1