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.
A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.
C# delegates are similar to pointers to functions, in C or C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime. Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.
A delegate is a method with a parameter and a return type. A delegate is a type that safely encapsulates a method. Delegates are object-oriented, type safe, and secure.
From http://msdn.microsoft.com/en-us/library/system.delegate.aspx:
The
Delegate
class is the base class for delegate types. However, only the system and compilers can derive explicitly from theDelegate
class or from theMulticastDelegate
class. It is also not permissible to derive a new type from a delegate type. TheDelegate
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 theMulticastDelegate
class; therefore, users should use thedelegate
keyword provided by the language.
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 to delObject.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With