Delegates and events both play an important role in the event-driven program. The delegates can refer to a method, and it is quite similar to the function pointer in C++. Events notify that some action has been performed. The basic difference between delegates and events is that delegates hold the reference of the methods and event provides a way to access that method using delegates.
Content: Delegates Vs Events in C#
|Basis for Comparison||Delegates||Events|
|Basic||A delegate holds the reference of a method.||The event is an over-layered abstraction provided to the delegates.|
|Syntax||delegate return_type delegate_name(parameter_list);||event event_delegate event_name;|
|Keyword||A delegate is declared using a keyword "delegate."||An Event is declared using a keyword "event".|
|Declaration||A delegate is declared outside any class.||An event is declared inside a class.|
|Invoke||To invoke a method it has to be referred to the delegate.||To invoke a method it has to be assigned to the event.|
|Covariance and Contravariance||They provide flexibility to the delegates.||No such concept.|
|Event Accessor||No such concept.||Manages the list of the event handlers.|
|Dependency||Delegates are independent of events.||The event can not be created without delegates.|
Definition of Delegates
In C# delegates are used as a function pointer to refer a method. It is specifically an object that refers to a method that is assigned to it. The same delegate can be used to refer different methods, as it is capable of holding the reference of different methods but, one at a time. Which method will be invoked by the delegate is determined at the runtime. The syntax of declaring a delegate is as follow:
delegate return_type delegate_name(parameter_list);
Here, the return_type declares the type of value returned by the method, that is called by delegate and delegate_name is the name of the delegate. The parameter_list define the list of parameters required by the methods that will be called by the delegate.
There are some important points to be remembered about the delegates. It can call any method that matches to its signature and the return type. A delegate can call both an instance method or a static method. If it calls an instance method it has to take the help of object to invoke the method. Delegates support multicasting i.e. you can chain up the methods that will be automatically called when the delegate referring to them is invoked. You simply have to create a delegate object and assign the methods one after other to the chain using ” += ” operator. You can also unchain a method using “-=” operator.
The features that add extra flexibility to the delegate are Covariance and Contravariance. Covariance and Contravariance permit the condition where the return type and the signature of the method assigned to delegate are different from that of the delegate.
- Covariance allows you to assign a method to the delegate where the return type of the method is a class which is derived from the class that specifies the return type of the delegate.
- Contravariance allows you to assign a method to the delegate where the parameter type of the method is a base class of the class that is specified as the parameter of the delegate.
Definition of Events
Events are the action performed which changes the state of an object. Events are declared using delegates, without the presence of delegates you can not declare events. You can say that an event provides encapsulation to the delegates. There are two important compounds when dealing with events that are event and event handler. If the objects are interested in the event, then it registers an event handler for that particular event. And whenever the events are fired the registered event handlers are called. The declaration of the event is as follow:
event event_delegate event_name;
Here, “event” is a keyword which shows the declaration of an event. Next, event_delegate show the name of the delegate that is supporting the event. Then, event_name is the name of the event
There are some important points to be remembered while dealing with the events. Like delegates, events can also be multicast i.e. multiple objects which are chained up (registered) to an event handler, respond when an event is raised. The event handlers can be added to the chain using the operator “+=” and can be unchained using the operator “-=” Both static and instance method can be used as an event handler. The event accessor can give you the control of the implementation of event handler list.
The interface can contain events. Events can also be abstract whereas accessor based event can not be abstract. The event can be virtual and be overridden by derived class.
Key Differences Between Delegates and Events in C#
- Delegate is an object used as a function pointer to hold the reference of a method. On the other hand, events provide an abstraction to delegates.
- A keyword required to declare a delegate is a delegate whereas, a keyword required to declare an event is event.
- A delegate is declared outside a class whereas, an event is declared inside a class.
- To invoke a method using a delegate object, the method has to be referred to the delegate object. On the other hand, to invoke a method using an event object the method has to be referred to the event object.
- Covariance and Contravariance provide extra flexibility to the delegate objects. On the other hand, event has no such concepts.
- Event Accessor handles the list of event handlers whereas delegate has no such concept.
- Delegates are independent on events but, events can not be created without delegate.
Delegates are useful as they support events, and they provide the facility of executing the method at runtime. The event accessor allows us to synchronize the event handlers in multithreading applications.