In software development, interaction and communication between objects are essential for building responsive and modular applications.
The .NET Framework provides a robust mechanism for this — called Events.
Introduction
In simple terms, an Event in the .NET Framework is a notification sent by an object when a specific action occurs.
It allows one object (called the publisher) to notify other objects (called subscribers) that something has happened.
Think of an event like a doorbell:
When someone presses the bell button (event trigger),
The bell rings (event raised),
And you hear it and respond (event handled).
What Is an Event?
In C#, an Event is a member of a class that is used to provide notifications to other classes or objects when something of interest occurs.
Events are built on top of delegates, which define the method signature for the event handler.
Basic Syntax
public delegate void SampleEventHandler(string message);
public event SampleEventHandler SampleEvent;
Here, the event SampleEvent is declared based on the delegate SampleEventHandler.
Components of an Event
| Component | Description |
|---|---|
| Event | The action or occurrence (like a click or key press). |
| Publisher | The class that declares and raises the event. |
| Subscriber | The class that receives and handles the event. |
| Delegate | Defines the method signature for the event handler. |
Example: Custom Event in C#
Let’s see how events work with a simple example.
using System;
public class Button
{
// Step 1: Declare a delegate
public delegate void ClickHandler(string message);
// Step 2: Declare an event using that delegate
public event ClickHandler OnClick;
// Step 3: Method to raise the event
public void Click()
{
Console.WriteLine("Button clicked!");
OnClick?.Invoke("Hello, Sandhiya! The button was clicked.");
}
}
public class Program
{
static void Main()
{
Button btn = new Button();
// Step 4: Subscribe to the event
btn.OnClick += ShowMessage;
// Step 5: Trigger the event
btn.Click();
}
// Event Handler Method
static void ShowMessage(string message)
{
Console.WriteLine($"Event Handler Received: {message}");
}
}
Output
Button clicked!
Event Handler Received: Hello, Sandhiya! The button was clicked.
How It Works
The publisher (Button class) defines and raises an event.
The subscriber (Program class) registers a method to handle it.
When the event is triggered, the delegate invokes the subscribed methods automatically.
Built-in Events in .NET Framework
.NET provides many predefined events, especially in Windows Forms, WPF, and ASP.NET controls.

Join the conversation! Your thoughts help the community grow.