Simple Delegate (delegate) vs. Multicast delegates

I have gone through many articles but I am still not clear about the difference between the normal delegates that we usually create and multicast delegates.

public delegate void MyMethodHandler(object sender);
MyMethodHandler handler = new MyMethodHandler(Method1);
handler += Method2;
handler(someObject);

The above delegate MyMethodHandler will call these two methods. Now where does multicast delegates come in. I have read that they can call multiple methods but I am afraid that my basic understanding about delegates is not correct.


Solution 1:

This article explains it pretty well:

delegate void Del(string s);

class TestClass
{
    static void Hello(string s)
    {
        System.Console.WriteLine("  Hello, {0}!", s);
    }

    static void Goodbye(string s)
    {
        System.Console.WriteLine("  Goodbye, {0}!", s);
    }

    static void Main()
    {
        Del a, b, c, d;

        // Create the delegate object a that references 
        // the method Hello:
        a = Hello;

        // Create the delegate object b that references 
        // the method Goodbye:
        b = Goodbye;

        // The two delegates, a and b, are composed to form c: 
        c = a + b;

        // Remove a from the composed delegate, leaving d, 
        // which calls only the method Goodbye:
        d = c - a;

        System.Console.WriteLine("Invoking delegate a:");
        a("A");
        System.Console.WriteLine("Invoking delegate b:");
        b("B");
        System.Console.WriteLine("Invoking delegate c:");
        c("C");
        System.Console.WriteLine("Invoking delegate d:");
        d("D");
    }
}
/* Output:
Invoking delegate a:
  Hello, A!
Invoking delegate b:
  Goodbye, B!
Invoking delegate c:
  Hello, C!
  Goodbye, C!
Invoking delegate d:
  Goodbye, D!
*/

Solution 2:

The C# specification states that all delegate types must be convertible to System.Delegate. In fact the way the implementation implements this is that all delegate types are derived from System.MulticastDelegate, which in turn derives from System.Delegate.

Is that clear? I'm not sure that answered your question.

Solution 3:

"All delegate instances have multicast capability." - http://msdn.microsoft.com/en-us/library/orm-9780596527570-03-04.aspx

"In C#, all delegate types support multicast" - http://msdn.microsoft.com/en-us/library/orm-9780596516109-03-09.aspx

Solution 4:

To clarify a bit: All delegates are instances of the class MulticastDelegate, regardless of whether they have one or multiple target methods. In principle there are no difference between a delegate with a single or multiple targets, although the runtime is optimized a bit towards the common case with a single target. (A delegate with 0 targets is not possible though, it is one or more.)

When you are instantiating a delegate like new MyMethodHandler(Method1), you create a delegate with a single target (the Method1 method).

Delegates with multiple targets are created by combining two existing delegates. The resulting delegate will have the sum of targets. Delegates can be combined explicitly with Delegate.Combine(), but also implicitly by using the += operator on an existing delegate, as in your example.

Invoking a delegate in turn calls every target in the delegate. So in your example handler(someObject); will call two methods (Method1 and Method2), since you have created a delegate with these two targets.