If you like to create custom delegates you would use the delegate
keyword in lowercase.
What can you do with the actual Delegate
Class? What is this good for? I don't understand the exact difference.
If you like to create custom delegates you would use the delegate
keyword in lowercase.
What can you do with the actual Delegate
Class? What is this good for? I don't understand the exact difference.
The delegate keyword is for the compiler to do some magic for you. When you declare a new delegate with a custom signature,
So now when you call
delObject(args)
- the compiler translates that todelObject.Invoke(args)
The Delegate base class provides some functionality such as
The C# compiler forbids you from deriving from Delegate explcitly in your code.. you have to use the delegate keyword.
The advantage of the Delegate class is that it is the base class for all delegate types in .Net. Having a method which takes an instance of this class allows you to operate generically over all manner of delegates. This is the reason operations like ISynchronizedInvoke.Invoke use this as a parameter.
Another neat thing you can do with
delegate
keyword is create delegates inline, without having to declare them, for example:One of the things the
Delegate
class can be used for is more control when invoking event handlers. For example, with normal event processing, an exception in any event handler will prevent any later event handlers from being called. You can alter that behavior by using theDelegate
class to manually invoke each event handler.From an implementation perspective, the Delegate class defines the fields used to represent a delegate's function pointer and the MultiCastDelegate class provides the base line functionality used by events. Also, as other people mentioned, Delegate provides the "DynamicInvoke" method which allows you to invoke any delegate.
From http://msdn.microsoft.com/en-us/library/system.delegate.aspx: