Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Delegate which isn't a MulticastDelegate in C#?

I think the answer is NO? If there isn't, why do we have separated Delegate and MulticastDelegate classes? Maybe it's again because of "some other .NET languages"?

like image 654
Cheng Chen Avatar asked Jan 17 '11 08:01

Cheng Chen


People also ask

What is a multi cast delegate?

The multicast delegate contains a list of the assigned delegates. When the multicast delegate is called, it invokes the delegates in the list, in order. Only delegates of the same type can be combined. The - operator can be used to remove a component delegate from a multicast delegate.

Are delegates immutable C#?

After a delegate is created, the method it is associated with never changes; delegate objects are immutable.

What is the difference between events and multicast delegates?

Delegates are pointer to functions and used for call back. Multicast delegates help to invoke multiple callbacks. Events encapsulate delegate and implement publisher and subscriber model.

Why do we use multicast delegates?

Multicasting of delegate is an extension of the normal delegate(sometimes termed as Single Cast Delegate). It helps the user to point more than one method in a single call. Properties: Delegates are combined and when you call a delegate then a complete list of methods is called.


3 Answers

EDIT: I thought this was part of ECMA 335, but I can't see it in there anywhere.

You can't create such a delegate type in C#, but you can in IL:

.class public auto ansi sealed Foo
       extends [mscorlib]System.Delegate
{
    // Body as normal
}

The C# compiler has no problems using such a delegate:

using System;

class Test
{
    static void Main()
    {
        Foo f = x => Console.WriteLine(x);
        f("hello");
    }
}

But the CLR does when it tries to load it:

Unhandled Exception: System.TypeLoadException: Could not load type 'Foo' from assembly 'Foo, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' because it cannot inherit directly from the delegate class. at Test.Main()

Basically the Delegate/MulticastDelegate separation is an historical accident. I believe that early alpha/beta versions did make the distinction, but it proved too confusing and generally not useful - so now every delegate derives from MulticastDelegate.

(Interestingly, the C# specification only mentions MulticastDelegate once, in the list of types which can't be used as generic constraints.)

like image 66
Jon Skeet Avatar answered Nov 13 '22 11:11

Jon Skeet


No, there isn't, because all delegates must naturally be able to be Delegate.Combineed. Delegate is there simply to wrap the non-multicasting functionality into a base class.

like image 25
user541686 Avatar answered Nov 13 '22 11:11

user541686


System.MuticastDelegate is derived from System.Delegate. Each level within the delegate hierarchy provides a different set of services. System.Delegate is a container of the data for what method to call on a particular object. With System.MulticastDelegate comes the additional capability of not only invoking a method on a single object, but on a collections of objects. This enables multiple subscribers to an event.

Not sure, i have answered your question.

like image 45
KBBWrite Avatar answered Nov 13 '22 11:11

KBBWrite