Events and Delegates

Multi-cast Delegates

A delegate is called Multi-cast Delegate that derives from the System.MulticastDelegate contains an invocation list with multiple methods. At times it is desirable to call two methods through a single delegate. This can be achieved through Single-cast Delegate but it is different from having a collection, each of which invokes a single method.

In Multi-casting you create a single delegate that will invoke multiple encapsulated methods. The return type of all the delegates should be same. Now the question why are we using Multi-cast delegates when Single-cast delegates are enough. Well the answer to this question is what if you want to call three methods when a button is clicked. The Multi-cast delegates are used with events where multiple call to different methods are required. System.MulticastDelegate contains two methods Combine and Remove. The Combine is a static method of class System.MulticastDelegate and is used to Combine the delegates and the remove method is used to remove the delegate from the list. For user convenience we have += operator overloaded for delegate Combine method and -= operator overloaded for Remove method Multi-cast delegates are similar to Single-cast delegates for e.g.

public delegate void MulticastDelegate();

Multi-cast delegate can have arguments and can return value as well. All the Methods pointed by  delegates should return the similar value as the delegate return type.

public delegate string MultiCastOne(string Name);

Consider a simple example:

using System;

namespace Multi_castDelegate
{
   /// <summary>
   /// Summary description for Class1.
   /// </summary>
   class MyClassDelegate
   {
       /// <summary>
       /// The main entry point for the application.
       /// </summary>
       
       public delegate string StringDelegate(string s);
   }
}

Below is the class that defines the static methods having same signature as delegate.

using System;

namespace Multi_castDelegate
{
   /// <summary>
   /// Summary description for MyImplementingClass.
   /// </summary>
   public class MyClass
   {
       public MyClass()
       {
           
       }

       public static string WriteString(string s)
       {
           Console.WriteLine("Writing string");
           return "null";
       }

       public static string logString(string s)
       {
           Console.WriteLine("loging string");  
           return "null";
       }

       public static string TransmitString(string s)
       {
           Console.WriteLine("Transmitting string");  
           return "null";
           
       }
   }
}

The Main class:

using System;
using System.Threading;
namespace Multi_castDelegate
{
   /// <summary>
   /// Summary description for Test.
   /// </summary>
   public class Test
   {
       public static void Main()
       {
           MyClassDelegate.StringDelegate
               Writer,Logger,Transmitter;

           MyClassDelegate.StringDelegate
               myDelegate;

           Writer=new
                  MyClassDelegate.StringDelegate(MyClass.WriteString);
           /// calling Writer
           Writer("hello i am Writer just acting like Single cast");
           Logger=new MyClassDelegate.StringDelegate(MyClass.logString);
           ///calling Logger
           Logger("hello i am Logger just acting like Single-cast");
           Transmitter=new MyClassDelegate.StringDelegate(MyClass.TransmitString);
           ///calling Transmitter
           Transmitter("hello i am Transmitter just acting like Single-cast");  
           ///here mydelegate used the Combine method of System.MulticastDelegate
           ///and the delegates combine  
           myDelegate=(MyClassDelegate.StringDelegate)System.Delegate.Combine(Writer,Logger);  
           myDelegate("used Combine");
           ///here Transmitter is also added using the overloaded form of Combine
           myDelegate+=Transmitter;
           myDelegate("Using Overloaded Form");
           ///now using the Remove method
           myDelegate=(MyClassDelegate.StringDelegate)System.Delegate.Remove(myDelegate,Writer);
           myDelegate("Without Writer");    
           ///overloaded Remove
           myDelegate-=Transmitter;
           myDelegate("Without Transmitter");
           System.Threading.Thread.Sleep(2300);    
           
       }
   }
}

The above program contains three classes, MyClassDelegate contains the delegate.

 public delegate string StringDelegate(string s);

The class MyClass Contains the static methods that contains the static methods that have a similar signature as the delegate StringDelegate. The third class is the Test Class which shows how to combine the delegates and how to remove the delegate from the list.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Linux is only free if your time has no value” - Jamie Zawinski