Compare the Difference Between Similar Terms

Difference Between Delegates and Events in C#

Key Difference – Delegates vs Events in C#
 

C# is a programming language developed by Microsoft. It supports Structured Programming and Object-Oriented Programming. Delegates and events are used for event-driven programming in C#.  A delegate is a type-safe pointer to a method. In an application with a Graphical User Interface (GUI), there are buttons etc. When a button is clicked, some kind of action occurs. Those actions are called events. For example, refreshing a web page will trigger the page load event. This article discusses the difference between delegates and events in C#. The key difference between delegate and event in C# is that a delegate is a reference to a method while an event is associated with an event handler using a delegate. When an event occurs, it sends a signal to the delegate. Then the delegate executes the corresponding function.  Hence, all events are depended on delegates.

CONTENTS

1. Overview and Key Difference
2. What are Delegates in C#
3. What are Events in C#
4. Similarities Between Delegates and Events in C#
5. Side by Side Comparison – Delegates vs Events in C# in Tabular Form
6. Summary

What are Delegates in C#?

Delegates in C# are similar to callback functions in other programming languages such as JavaScript. Callback functions are assigned at runtime. They provide functionality that can be switched in and out as required. The same can be implemented using delegates in C#. The delegates in C# decouples the caller and the called function. Therefore, it reduces the heavy coupling.

.NET framework classified types into two categories. They are value types and reference types. The structure is an example of value type. The class is an example of a reference type. Delegates are also referenced types. A delegate is used to hold a reference to a method. They are type-safe pointers. Delegated are derived from the System.Delegate class. Delegate declaration is similar to a method declaration but contains the keyword ‘delegate’.  The syntax of the delegate is as follows. delegate < return type> <delegate name> <parameter list>. The return type declares the return value by the method. It is called by the delegate. The parameter list defines the parameters required by the method. Refer the below program with delegates.

Figure 01: C# Program with Delegates

According to the above program, the methods calSquare and calCube is similar to the declared delegate. In the main program, the variable cal is assigned to the calSquare function. Now, cal is pointing to the calSquare method.  When passing the value, the square value is returned, and it is printed to the screen. Again, cal is assigned to the calCube method. Now, cal is pointing to the calCube method. When passing the value, the cube value is returned and printed to the screen. The same delegate is used to refer different methods. Therefore, it increases the program flexibility.

What are Events in C#?

In an application there can be there can be key presses, button clicks and mouse movements. When these occur, an event will have raised. When the submit button is pressed in a submission form, the button_click() event is raised in the background. The event calls an execute the associated function. When an event occurs, the application response to that. There are two sections in an event handling program. The publisher contains the definition of events and delegated. The Subscriber accepts the event and provides the event handler.

The declaration of the event is <modifier> event <type> <event name>; The type of an event declaration must be a delegate type, and the delegate must be as accessible as the event itself. The event name should be a valid C# name.

Figure 02: C# Program with Events

According to the above program, the delegate and the event are created. In the Calculate class, there is a method to add two numbers. If an odd number is found, the event will occur. It is the eventOddNumber (). In the main method, the eventOddNumber event handler calls the delegate. Finally, the delegate executes the method.

What is the Similarity Between Delegates and Events in C#?

What is the Difference Between Delegates and Events in C#?

Delegates vs Events in C#

A delegate is a reference type variable that holds the reference to a method. An event is a delegate type class member used by the object or class to provide notification to other objects that an event has occurred.
 Keyword
A delegate is declared using the keyword ‘delegate’. An event is declared using the “event” keyword.
Dependency
Delegates are undependable. Events are depended on delegates.

Summary – Delegates vs Events in C#

Delegates and events are two concepts associated with event-driven programming in C#. This article discussed the difference between delegates and events in C#. The difference between delegate and event in C# is that a delegate is a reference to a method while an event is associated with an event handler using a delegate. Events are depended on delegates. When an event occurred, it sends a signal to the delegate. Then the delegate executes the corresponding function

Reference:

1.Balagurusamy, E. Programming in C#, A premier. Fourth ed., McGraw Hill Education (India)Private Limited, 2016.
2.“Primary Menu.” C Tutorial. Available here 
3.sachinbpaul. Delegates in C#, Sachin Paul, 1 Oct. 2014. Available here