.NET  

What Are Events in .NET Framework?

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

ComponentDescription
EventThe action or occurrence (like a click or key press).
PublisherThe class that declares and raises the event.
SubscriberThe class that receives and handles the event.
DelegateDefines 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

  1. The publisher (Button class) defines and raises an event.

  2. The subscriber (Program class) registers a method to handle it.

  3. 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 / ClassEventDescription
ButtonClickTriggered when the button is clicked.
TextBoxTextChangedRaised when text changes.
FormLoadOccurs when a form is loaded.
TimerTickTriggered when a time interval elapses.
FileSystemWatcherChangedRaised 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

ConceptDescription
Full FormEvent (no acronym)
PurposeNotify subscribers when an action occurs
Base TypeDelegate
Declared ByPublisher class
Handled BySubscriber class
UsageCommon 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

  • button1_Click is the event handler.

  • Click is the event raised by the Button control.

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.