Multicast delegate with + and - - CSharp Custom Type

CSharp examples for Custom Type:delegate

Description

Multicast delegate with + and -

Demo Code

using System;//from w w  w  .j a  v a 2 s.  c  o m
class MulticastTester
{
   delegate void Greeting();
   public static void Main()
   {
      Greeting myGreeting = new Greeting(SayThankYou);
      Console.WriteLine("My single greeting:");
      myGreeting();
      Greeting yourGreeting = new Greeting(SayGoodMorning);
      Console.WriteLine("\nYour single greeting:");
      yourGreeting();
      Greeting ourGreeting = myGreeting + yourGreeting;
      Console.WriteLine("\nOur multicast greeting:");
      ourGreeting();
      ourGreeting += new Greeting(SayGoodnight);
      Console.WriteLine("\nMulticast greeting which includes Goodnight:");
      ourGreeting();
      ourGreeting = ourGreeting - yourGreeting;
      Console.WriteLine("\nMulticast greeting without your greeting:");
      ourGreeting();
      ourGreeting -= myGreeting;
      Console.WriteLine("\nSingle greeting without your greeting and my greeting:");
      ourGreeting();
   }
   public static void SayThankYou()
   {
      Console.WriteLine("Thank you!");
   }
   public static void SayGoodMorning()
   {
      Console.WriteLine("Good morning!");
   }
   public static void SayGoodnight()
   {
      Console.WriteLine("Goodnight");
   }
}

Result


Related Tutorials