How Events And Delegates Are Related

 Posted on 08/09/2018

Share On: facebook gplus twitter

In general, we call delegates function pointers,  meaning delegate object stores a reference of a method.

The delegate is a type that defines a signature, that is, the return value type and parameter list types for a method.

Delegate type can be used to declare a variable and associate its instance with any method with the same signature as the delegate then finally invoke (or call) the method through the delegate instance. Delegate allows us to implement different implementation in different methods (a kind of polymorphism - many forms)

Let’s have a look at the example to understand how delegate works.

Create a delegate that accepts two integer type variables and returns integer value.

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

Define methods that have the same signature as calculate but they carry out different operations like multiplication, subtraction and addition mentioned below,

  1. public static int Multiply(int x, int y)  
  2. {  
  3.     Console.WriteLine("Hello Multiply !!");  
  4.     Console.WriteLine("Multiply {0} and {1} gives result {2}", y, x, x * y);  
  5.     return x * y;  
  6. }  
  8. public static int Add(int x, int y)  
  9. {  
  10.     Console.WriteLine(" Add {0} and {1} gives result  {2}", y, x, x + y);  
  11.     return x + y;  
  12. }  
  14. public static int Subtract(int x, int y)  
  15. {  
  16.     Console.WriteLine("Subtract {0} from {1} gives result  {2}", y, x, x - y);  
  17.     return x - y;  

Declare & create the delegate object multiplyCalculate that references the method Multiply.

Calculate multiplyCalculate = new Calculate(Multiply);

Declare & create the delegate object subtractCalculate that references the method Subtract.

 Calculate subtractCalculate = new Calculate(Subtract); 

Declare & create the delegate object addCalculate that references the method Add, 

 Calculate addCalculate = new Calculate(Add); 

What Is an Event?

An event is a notification raised by one object to notify other objects that some action has happened. In C#, we define event using keyword “event” whereas in VB.Net we define event using keyword “Event” 

  1. public event EventHandler DatabaseConnectionOpened;  

If we look at the declaration, we easily make out public is the access modifier, event is the keyword used to define an event, “DatabaseConnectionOpened” is the event name but what is EventHandler?

The answer is: EventHandler is a type that represents reference to method with a particular parameter list and return type. Doesn't it look like we are talking about some specific object? Yes, it is a Delegate.

So if we look at the event declaration closely, we can say that event named “DatabaseConnectionOpened” is a type of EventHandler but that’s not true actually. EventHandler is a delegate and it is associated with the event named “DatabaseConnectionOpened” to hold a reference of a method to be called when the event is raised .


Add Comment: