In this article we will try to understand what is delegate, how to use it.

Dictionary meaning:A person sent or authorized to represent others “or “A person designated to act for or represent another or others

History of Delegate in C#
The diagram shows the history of delegates by C# version,

delegates_ev

A delegate is a class that can hold a reference (source of information) to a method. We can say that delegate is like a pointer to a function.

Every delegate object is implicitly derived from class Delegate.

Declaration of Delegate:
A delegate can be declared using delegate keyword followed by a function signature as shown below:

Signature of a Single Cast Delegate:

Example:

Delegate class has four steps process for defining and using delegates – Declaration, Reference, Target and Invoke.

The first step is to declare the delegate with the return type and input parameters.

Which is having same signature as method below.

The Second step is to create a delegate reference.

The third step is to target/point the delegate reference to an object instance.

The final step is to invoke the delegate, like as invoking a regular method.

Finally the Sample Code:

Output: Sum of two integer is 30;

Let’s do it with Anonymous Method:

Output will be the same as before, this time we have made a little bit change to point the method.Here we use an annymous method, let’s take a look how:

And the Sum method is no more exist in the DelegateClass

Types of delegates:

  1. Single cast delegate
  2. Multi cast delegate

Single Cast Delegate:
A single cast delegate holds the reference of only single method. This type of delegate is derive from the System.Delegate class.

Note: Our previous Example is a single cast delegate

Multi Cast Delegate:
The delegate can points to multiple methods. Class MulticastDelegate derives from Delegate and represents a delegate that can invoke more than one method at once.

Internally, MulticastDelegate is implemented as a linked list of delegates.

Add/Remove Delegates:
All delegate types declared in C# have implicit operators += and -=. The “+” operator adds a function to the delegate object and the “-” operator removes an existing function from a delegate object. 

Sample Example:

Output:
Sum of two integer is 25;
Divide of two integer is 4; not showing this time.
Multiply of two integer is 100;

Key Notes:

  1. Delegates are reference type.
  2. Delegates are type-safe.
  3. Delegates encapsulates one or more methods.
  4. We can invoke all the methods the delegate is encapsulating with a single call
  5. Delegates allow methods to be passed as parameters.
  6. Invoking a delegate is like as invoking a regular method.
  7. Delegates are used in event handling for defining callback methods.
  8. Once a delegate is created, the method it is associated will never changes because delegates are immutable in nature.
  9. Delegates provide a way to execute methods at run-time.
  10. All delegates are implicitly derived from System.MulticastDelegate, class which is inheriting from System.Delegate class.

Leave a Reply

Your email address will not be published. Required fields are marked *