2

I have been reading a bit about delegates in depth, it is confusing that a delegate with one method could be different than a multicast delegate. However, via reflection, you can see plainly that even with only a single method, a delegate is indeed deriving from MulticastDelegate, and not immediately deriving from a Delegate object.

class Program 
{
    public delegate void MyDelegate();

    static void SomeMethod()
    {
    }

    static void Main(string[] args)
    {
        MyDelegate del = null;
        del = new MyDelegate(SomeMethod);
        Console.WriteLine(del.GetType().BaseType.Name);            
        Console.ReadKey();
    }
}

Output:MulticastDelegate

I realize that a MulticastDelegate contains an invocation list of Delegate objects. I am wondering if it is possible to create a single Delegate directly and if there would be any advantage to doing so, other than calling GetInvocationList() and extracting the Delegate objects individually.

user2255673
  • 237
  • 1
  • 3
  • 9
  • 1
    Where is it "universally taught"? I've never seen that. – Jon Skeet Apr 25 '13 at 16:35
  • Every article I read differentiates a single delegate from a multicast delegate as they are different entities. However, I don't see how it's possible to create a single delegate in a straightforward manner, and all delegates are multicast. – user2255673 Apr 25 '13 at 16:37
  • 2
    Every *recent* (like post 2003) article I've read talks about single-cast delegates to start with, then explains that they're all really multi-cast (instances of MulticastDelegate), but it's helpful to *start* with single-cast just for simplicity. If you have any examples where it really *is* taught that "a delegate with one method is not a multicast delegate" you should provide examples rather than generalities. – Jon Skeet Apr 25 '13 at 16:51
  • 2
    I apologize. I don't think about generalizing. The web is abound with literally countless articles. Nearly all that I come across are still using this distinction between single and multicast, as if you are actually creating single delegates. It's a minor technicality, but I wanted to know just to be thorough. – user2255673 Apr 25 '13 at 16:57
  • If you've seen so many, then presumably you can give concrete examples easily, right? So edit them into the question. – Jon Skeet Apr 25 '13 at 17:00
  • thecoop answered my question, and I edited my phrasing for posterity and political correctness. – user2255673 Apr 25 '13 at 17:04
  • 1
    It's not a matter of political correctness. There would have been all kinds of benefits in you listing some concrete examples: we could have seen *exactly* what was said, and hopefully mailed authors with corrections. The question is better without any reference to incorrect articles if there aren't concrete examples though. – Jon Skeet Apr 25 '13 at 17:06

1 Answers1

8

Not really. All .NET delegates are derived from MulticastDelegate. When .NET was first written, there was originally a difference between single & multicast, but that distinction was dropped before release. However, the underlying types weren't merged into one.

You can't derive from Delegate directly in C#. You might be able to in raw IL, but there's not really much point, as a MulticastDelegate operates like a singlecast delegate to all intents and purposes.

thecoop
  • 45,220
  • 19
  • 132
  • 189
  • 4
    You can clarify it a bit by pointing out that Delegate is an abstract class. So you can never create an instance of it. – Hans Passant Apr 25 '13 at 17:26