C# MulticastDelegate and Invoke Method

982 Views Asked by At

I was thinking on MulticastDelegate and Delegate, and i dont understand why the reason that invoke method arent an abstract method of MulticastDelegate. Someone knows?

1

There are 1 best solutions below

0
On

The MulticastDelegate type has no Invoke() method (nor BeginInvoke()). These methods are automatically provided in the actual delegate type by the runtime. So the most obvious reason to the question "why isn't the Invoke() method in MulticlassDelegate marked abstract" is that there is no such method.

If you are asking why that method is not in MulticlassDelegate (and then marked abstract there), then I would ask you: how could it be? Each delegate type needs its own Invoke() method, because the signature of that method depends specifically on the signature of the delegate type. No base class could provide that type.

Finally, note that the MulticlassDelegate type is one of several "special" types in .NET, and is explicitly intended not to be inherited by user code. Not it, nor any declared delegate subclass of the type. Since inheritance is not possible, it really doesn't matter whether a member is abstract; it wouldn't do anyone any good even if it were.

From the documentation:

The Delegate class is the base class for delegate types. However, only the system and compilers can derive explicitly from the Delegate class or from the MulticastDelegate class. It is also not permissible to derive a new type from a delegate type. The Delegate class is not considered a delegate type; it is a class used to derive delegate types.

Most languages implement a delegate keyword, and compilers for those languages are able to derive from the MulticastDelegate class; therefore, users should use the delegate keyword provided by the language.

  • Note:

    The common language runtime provides an Invoke method for each delegate type, with the same signature as the delegate. You do not have to call this method explicitly from C#, Visual Basic, or Visual C++, because the compilers call it automatically. The Invoke method is useful in reflection when you want to find the signature of the delegate type.