articles

home / developersection / articles / c# events – a complete beginner's guide

C# Events – A Complete Beginner's Guide

C# Events – A Complete Beginner's Guide

Anubhav Kumar 909 22-Apr-2025

In C#, events are a powerful way to implement the publish-subscribe pattern. They allow one object to notify another when something happens.

For example, a button click, a file download completed, or a value change can be raised as events.

What is an Event?

An event is a notification sent by an object when an action occurs. Other objects can subscribe to these events and respond accordingly.

Real-world Analogy:

When the doorbell rings (event), the person inside (subscriber) gets notified and reacts.

Events Involve Two Main Components:

  • Publisher – The class that raises the event.
  • Subscriber – The class that responds to the event.

Syntax of Events

Events in C# are usually based on delegates.

public delegate void MyEventHandler(string message);

public class Publisher
{
    public event MyEventHandler OnPublish;

    public void TriggerEvent()
    {
        OnPublish?.Invoke("Hello from Publisher!");
    }
}

Example: Basic Event Usage

using System;

class Publisher
{
    public event Action OnDataPublished;

    public void PublishData()
    {
        Console.WriteLine("Publishing data...");
        OnDataPublished?.Invoke(); // Raise the event
    }
}

class Subscriber
{
    public void HandleData()
    {
        Console.WriteLine("Subscriber received the data.");
    }
}

class Program
{
    static void Main()
    {
        Publisher publisher = new Publisher();
        Subscriber subscriber = new Subscriber();

        // Subscribe to the event
        publisher.OnDataPublished += subscriber.HandleData;

        publisher.PublishData();
    }
}

Output: 

Publishing data...
Subscriber received the data.

Unsubscribing from Events

You can unsubscribe from events using -= operator to avoid memory leaks:

publisher.OnDataPublished -= subscriber.HandleData;

Event with Parameters (Using EventHandler)

You can pass data with events using EventHandler<T>.

public class DataEventArgs : EventArgs
{
    public string Data { get; set; }
}

public class Publisher
{
    public event EventHandler<DataEventArgs> DataPublished;

    public void Publish(string data)
    {
        DataPublished?.Invoke(this, new DataEventArgs { Data = data });
    }
}

class Subscriber
{
    public void OnDataReceived(object sender, DataEventArgs e)
    {
        Console.WriteLine("Data Received: " + e.Data);
    }
}

Why Use Events Instead of Direct Method Calls?

Direct Call Event-based Call
Tightly coupled Loosely coupled
Hard to extend Easily extensible (multiple handlers)
Less flexible More reusable and scalable

Summary

Feature Description
Purpose Notify other classes when something happens
Based On Delegates
Components Publisher (raises) & Subscriber (handles)
Built-in Type EventHandler / EventHandler<T>
Subscription += to attach, -= to detach
Usage UI events, system notifications, messaging

Events are crucial in building interactive, responsive, and decoupled systems — especially in GUI apps, services, and libraries.

Want to explore custom events, async event handlers, or how events work in WinForms/WPF?

Read More -

 


c# c# 
Updated 22-Apr-2025
Anubhav Kumar

Student

The Anubhav portal was launched in March 2015 at the behest of the Hon'ble Prime Minister for retiring government officials to leave a record of their experiences while in Govt service .

Leave Comment

Comments

Liked By