Shiman’s Weblog

Collecting stones from the sea-shore.

C# .NET Delegates : System.MulticastDelegate Class

Delegates default to multicast delegates, which inherit from the System.MulticastDelegate class. A multicast delegate is similar to a basket. Multiple function pointers or delegates can be dropped into the basket. The list of delegates is stored in an invocation list, which can even include multiple instances of the same delegate. When you execute the contents of the basket, the function pointers of the delegates are called FIFO. Multicast delegates are useful for invoking a chain of functions.

Combining delegates

Multiple delegates are combined using the Combine method, the plus operator (+), or the += assignment operator. Combine is a static method. In C#, the Combine method is called with two delegate parameters or an array of delegates. The method returns a reference to the combined delegate.

The following code combines two delegates separately wrapping functions MethodA and MethodB. When the Combine delegate is invoked, MethodA is run first and MethodB second. This is the order in which the function pointers are added to the multicast delegate. Both the Combine method and the += assignment operators are shown, with the Combine statement commented to avoid duplication.

using System;

namespace Examples.Delegates
{
    public delegate void DelegateClass();
    public class Program
    {
        public static void Main()
        {
            DelegateClass del = MethodA;
            del += MethodB;
            //del=(DelegateClass) DelegateClass.Combine(
            // new DelegateClass [] {MethodA, MethodB});
            del();

            Console.ReadKey();
        }
        public static void MethodA()
        {
            Console.WriteLine("MethodA...");
        }

        public static void MethodB()
        {
            Console.WriteLine("MethodB...");
        }
    }
}

Removing delegates

To remove delegates from a multicast delegate, use the Remove method, the minus operator (-), or the -= assignment operator. Remove is a static method that accepts the source delegate as the first parameter and the delegate to remove as the second parameter. Be careful not to inadvertently remove all delegates from a multicast delegate, which causes a run-time error when invoked. The following code removes MethodB from a multicast delegate. When the delegate is invoked, MethodA is invoked, but not MethodB. The three methodologies are shown, with the Remove statement and operator minus commented to avoid duplication. 

using System;

namespace Examples.Delegates
{
    public delegate void DelegateClass();

    public class Program
    {
        public static void Main()
        {
            DelegateClass del = MethodA;
            del += MethodB;
            del += MethodC;
            del = del - MethodB;

            // del = (DelegateClass) DelegateClass.Remove(del, (DelegateClass) MethodB);
            // del -=MethodB;

            del();

            Console.ReadKey();
        }
        public static void MethodA()
        {
            Console.WriteLine("MethodA...");
        }

        public static void MethodB()
        {
            Console.WriteLine("MethodB...");
        }

        public static void MethodC()
        {
            Console.WriteLine("MethodC...");
        }
    }
}

August 18, 2008 Posted by shiman | C#.Net, Computer Science, OOP, Programming | , , , , , , , , , | No Comments Yet