Difference Between Delegates and Events in C# (with Comparison Chart)

November 2022 · 5 minute read

delegates-and-eventsDelegates 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#

  • Comparison Chart
  • Definition
  • Key Differences
  • Conclusion
  • Comparison Chart

    Basis for ComparisonDelegatesEvents
    BasicA delegate holds the reference of a method.The event is an over-layered abstraction provided to the delegates.
    Syntaxdelegate return_type delegate_name(parameter_list);event event_delegate event_name;
    KeywordA delegate is declared using a keyword "delegate."An Event is declared using a keyword "event".
    DeclarationA delegate is declared outside any class.An event is declared inside a class.
    InvokeTo 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 ContravarianceThey provide flexibility to the delegates.No such concept.
    Event AccessorNo such concept.Manages the list of the event handlers.
    DependencyDelegates 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.

    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.
  • Conclusion

    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.

    ncG1vNJzZmislZi1pbXFn5yrnZ6YsrR6wqikaJyZm7OmvsSnmp5lkprBuLHEp2SdnZyatKLAxKxkmqaUYrK3sc2tqmahnmKwbr%2FHmqmpZpipuq0%3D