Delegates and Events in C#

Delegates and Events in C#

Tudip

Tudip

18 March 2020

Delegates and Events in C#

What is Delegate

  • It is the same as function pointer in C and C++.
  • It is type-safe as it gives compile-time error when referencing any method. 
  • It holds the reference of a method and then calls that method for execution whenever the delegate is invoked.

Delegate Signature:

public delegate int DelegateMethod(int x, int y);  

Function with the same delegate’s signature that contains return type and the parameters, can be assigned to that delegate.

Why to use Delegate

  • We as developers often need to pass a function as a parameter to another function. For this purpose, we create and use them.
  • It is a class that encapsulates a method signature.

Types

  • SingleCast
    • This type of delegate holds the reference of only a single method and they are derived from System.Delegate class.

image-1.png

  • MultiCast
    • This type of delegate holds the reference of more than one method and they are derived from System.MulticastDelegate class.
    • The + and += operators are used to combine delegate instances.
    • The – and -= operators are used to remove delegate instances.

image-2.png

To get rid of all above steps 

  1. Creating a delegate
  2. Creating a Method to perform some action
  3. Referencing the method to that delegate.
  4. We can skip the second step and third step by using anonymous functions or using lambda functions.

AnonymousMethods and LambdaExpression

image-3.png

Generic Delegates

  • They were introduced as part of .NET Framework 3.5 which doesn’t require defining the delegate instance in order to invoke the methods

 Types

  • Func
    • It takes 0 or more input parameters upto 16 and returns a single value.
    • It does not allow ref and out parameters.
    • It can be used with anonymous methods and lambda expressions.
    • Syntax: Func<int, int, int> Sum = reference_method_name;
    • reference_method_name can be method name to add reference or it can be anonymous methods or lambda expressions.
  •   Action
    • It  takes 0 or more input parameters upto 16 and does not return any value.
    • It can be used with anonymous methods and lambda expressions.
    • Syntax: Action<int> display reference_method_name;
    • reference_method_name can be method name to add reference or it can be anonymous methods or lambda expressions.
  • Predicate
    • It takes one or more input parameters and returns bool value.
    • It is mainly used for checking conditions.
    • Syntax: Predicate<string> isLower = reference_method_name;
    • reference_method_name can be method name to add reference or it can be anonymous methods or lambda expressions.

Events:

An event is some action that is going to happen. 

For example, Tudip organizes an event for the employees, to make them aware about the features of new or existing products. 

Tudip notifies the employees about the event by email or other options.

In this scenario, Tudip is a publisher who organizes (raises) the event and notifies the employees about that and employees are the subscribers of that event and to attend the event is likely to handle the event.

Events are the important part in user interfaces and programming notifications. 

Events and the delegate work together. It is a reference to the delegate that means when an event is raised, the linked delegate gets called. 

When to use Events

When the code from different classes needs communication between them that means when something happens in one class or at some part of code and another part of the code needs a notification, events are used. event and delegate works hand-in-hand.

Syntax:

public event DelegateHandler delegateEvent;

Here the DelegateHandler is the delegate and is like  public delegate void DelegateHandler(); 

And we can linked the method with the event as follows:

  delegateEvent += new DelegateHandler(method_name); 

EventHandler Syntax:

public event EventHandler delegateEvent;

Events without delegates?

No, events use delegates internally so it is required but can be skipped if we use EventHandler with the event keyword while creating an event that does not need to create a delegate for that event as the EventHandler uses the delegate internally.

Event Handlers return void and take two input parameters.

The first parameter of Event Handler is source of event that means the sender and the second parameter of Event Handler is the object derived from EventArgs.

 

search

Blog Categories

Request a quote