Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegate vs. delegate keyword

Tags:

.net

delegates

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.

like image 483
Houman Avatar asked Apr 22 '09 20:04

Houman


People also ask

What is delegate in C# and why it is used?

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.

What is a delegate C sharp?

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.

What is difference between delegate and method in C#?

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.


2 Answers

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 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.

like image 76
LukeH Avatar answered Sep 20 '22 21:09

LukeH


The delegate keyword is for the compiler to do some magic for you. When you declare a new delegate with a custom signature,

  • the compiler creates a new Type for you derived from MulticastDelegate (which in turn derives from Delegate).
  • the compiler adds an Invoke method with your custom signature
  • similarly the compiler adds BeginInvoke and EndInvoke methods for this new type

So now when you call delObject(args) - the compiler translates that to delObject.Invoke(args)

The Delegate base class provides some functionality such as

  1. CreateDelegate (for obtaining a delegate wrapping a static/instance method)
  2. DynamicInvoke (to invoke a delegate with a list of arguments - late bound)
  3. Combine and Remove (for delegate chaining.. chain multiple delegates together e.g. multiple event handler delegates for an event)

The C# compiler forbids you from deriving from Delegate explcitly in your code.. you have to use the delegate keyword.

like image 30
Gishu Avatar answered Sep 22 '22 21:09

Gishu