VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/events-and-event-handlers-in-c-sharp/

⇱ Events and Event Handlers in C# - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Events and Event Handlers in C#

Last Updated : 6 May, 2026

Events in C# provide a way for objects to communicate with each other. They are based on the publisher-subscriber model, where one class (publisher) notifies other classes (subscribers) when something of interest occurs. Events are built on top of delegates and form a key part of event-driven programming.

Event

An event is a mechanism for sending notifications from one object to another. It is declared in a class and is triggered when a specific action happens. Events rely on delegates to point to the method that will handle the occurrence.

Syntax for Declaring an Event:

public event EventHandler EventName;

Key points

  • Events are declared using delegates.
  • The event keyword restricts delegate access, allowing only subscription (+=) and unsubscription (-=) from outside the class, while only the declaring class can raise (invoke) the event.
  • Events support loose coupling between components.

Event Handler

An event handler is a method that responds to an event. It contains the code to be executed when the event is raised. Event handlers are usually void methods with two parameters:

  • The sender (object that raised the event).
  • Event data (information about the event, derived from EventArgs).

Syntax:

public delegate void EventHandler(object sender, EventArgs e);

EventHandler is a delegate. To subscribe to this event a class must have a method with void return type and two input parameters of types :

  • object sender: the source (publisher) that raised the event.
  • EventArgs e: an object containing event data. If no data is required, EventArgs.Empty is used.

Declaring and Using Events

Step 1: Define a Delegate

A delegate specifies the signature of the event handler methods.

Step 2: Declare an Event

The event is declared in a class using that delegate.

Step 3: Subscribe and Handle the Event

Output:

Process Started...

Process Completed!

Here, the Process class is the publisher and ProcessCompletedHandler is the subscriber. The event is raised once the process finishes.

Using the Built-in EventHandler Delegate

Instead of defining custom delegates, C# provides EventHandler (no extra data) and EventHandler<TEventArgs> (with custom data). This is the recommended approach.

Output:

Task Running...

Task Completed Event Fired!

Here, TaskCompleted is raised by TaskManager. The handler method Manager_TaskCompleted executes when the event fires.

Events with EventArgs (Passing Data)

When you need to pass additional information, create a custom EventArgs class.

Output:

Downloading example.txt...

Download Finished: example.txt

Now the event not only notifies subscribers but also provides useful information (FileName) about the completed download.

Important Notes

  • Use EventHandler and EventHandler<TEventArgs> whenever possible instead of custom delegates.
  • Always check for null (?.Invoke) before invoking events to avoid exceptions.
  • Events promote encapsulation because they can only be raised by the declaring class or its derived classes (typically through protected methods).
  • Multiple subscribers can listen to the same event.
Comment
Article Tags:

Explore