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.
| Control / Class | Event | Description |
|---|
| Button | Click | Triggered when the button is clicked. |
| TextBox | TextChanged | Raised when text changes. |
| Form | Load | Occurs when a form is loaded. |
| Timer | Tick | Triggered when a time interval elapses. |
| FileSystemWatcher | Changed | Raised when a file or folder changes. |
EventHandler and EventArgs
The .NET Framework provides built-in delegates for standard event patterns:
Event Without Data
public event EventHandler MyEvent;
Event With Data
public event EventHandler<MyEventArgs> MyEvent;
Here, MyEventArgs is a custom class derived from EventArgs to pass additional information.
Advantages of Using Events
Promotes loose coupling between classes
Improves code reusability and scalability
Encourages separation of concerns
Simplifies communication between UI controls and backend logic
Important Points
Events can only be invoked by the declaring class (publisher).
You can subscribe (+=) and unsubscribe (-=) from events.
Multiple handlers can be attached to a single event.
Summary
| Concept | Description |
|---|
| Full Form | Event (no acronym) |
| Purpose | Notify subscribers when an action occurs |
| Base Type | Delegate |
| Declared By | Publisher class |
| Handled By | Subscriber class |
| Usage | Common in UI, asynchronous, and notification systems |
Real-World Example
In Windows Forms applications:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("You clicked the button!");
}
Here
Conclusion
Events are a fundamental part of the .NET Framework that make your applications interactive and responsive.
They enable communication between objects, simplify code, and provide a clean, decoupled design.
Whether you’re building desktop, web, or mobile apps — mastering events helps you handle user actions and system responses efficiently.