Mega Code Archive

 
Categories / C# / Language Basics
 

Define your own delegate

/* Learning C#  by Jesse Liberty Publisher: O'Reilly  ISBN: 0596003765 */  using System;  namespace DelegatesAndEvents  {      class MyClassWithDelegate      {          // The delegate declaration.          public delegate void StringDelegate(string s);      }      class MyImplementingClass      {          public static void WriteString(string s)          {              Console.WriteLine("Writing string {0}", s);          }          public static void LogString(string s)          {              Console.WriteLine("Logging string {0}", s);          }          public static void TransmitString(string s)          {              Console.WriteLine("Transmitting string {0}", s);          }      }     public class TesterDelegatesAndEvents     {        public void Run()        {            // Define three StringDelegate objects.            MyClassWithDelegate.StringDelegate                Writer, Logger, Transmitter;            // Define another StringDelegate            // to act as the multicast delegate.            MyClassWithDelegate.StringDelegate                myMulticastDelegate;            // Instantiate the first three delegates,            // passing in methods to encapsulate.            Writer = new MyClassWithDelegate.StringDelegate(                MyImplementingClass.WriteString);            Logger = new MyClassWithDelegate.StringDelegate(                MyImplementingClass.LogString);            Transmitter =                new MyClassWithDelegate.StringDelegate(                MyImplementingClass.TransmitString);            // Invoke the Writer delegate method.            Writer("String passed to Writer\n");            // Invoke the Logger delegate method.            Logger("String passed to Logger\n");            // Invoke the Transmitter delegate method.            Transmitter("String passed to Transmitter\n");            // Tell the user you are about to combine            // two delegates into the multicast delegate.            Console.WriteLine(                "myMulticastDelegate = Writer + Logger");            // Combine the two delegates;  assign the result            // to myMulticastDelegate            myMulticastDelegate = Writer + Logger;            // Call the delegated methods; two methods            // will be invoked.            myMulticastDelegate(                "First string passed to Collector");            // Tell the user you are about to add            // a third delegate to the multicast.            Console.WriteLine(                "\nmyMulticastDelegate += Transmitter");            // Add the third delegate.            myMulticastDelegate += Transmitter;            // Invoke the three delegated methods.            myMulticastDelegate(                "Second string passed to Collector");            // Tell the user you are about to remove            // the Logger delegate.            Console.WriteLine(                "\nmyMulticastDelegate -= Logger");            // Remove the Logger delegate.            myMulticastDelegate -= Logger;            // Invoke the two remaining delegated methods.            myMulticastDelegate(                "Third string passed to Collector");        }        [STAThread]        static void Main()        {           TesterDelegatesAndEvents t = new TesterDelegatesAndEvents();           t.Run();        }     }  }