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.
Define methods that have the same signature as calculate but they carry out different operations like multiplication, subtraction and addition mentioned below,
Declare & create the delegate object multiplyCalculate that references the method Multiply.
Declare & create the delegate object subtractCalculate that references the method Subtract.
Declare & create the delegate object addCalculate that references the method 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”
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 .