---
title: "C# Events – A Complete Beginner's Guide"  
description: "events are a powerful way to implement the publish-subscribe pattern. They allow one object to notify another when something happens."  
author: "Anubhav Sharma"  
published: 2025-04-22  
updated: 2025-04-22  
canonical: https://www.mindstick.com/articles/339102/c-sharp-events-a-complete-beginner-s-guide  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# C# Events – A Complete Beginner's Guide

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](https://www.mindstick.com/forum/572/pdf-file-download-in-zip-file-in-mvc-4) 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#](https://www.mindstick.com/articles/11963/delegate-and-events-in-c-sharp) are usually based on **delegates**.

```cs
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

```cs
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:

```plaintext
Publishing data...
Subscriber received the data.
```

## Unsubscribing from Events

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

```cs
publisher.OnDataPublished -= subscriber.HandleData;
```

## Event with Parameters (Using EventHandler)

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

```cs
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](https://www.mindstick.com/forum/161717/how-would-you-design-a-loosely-coupled-system-using-dot-net) |
| 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](https://answers.mindstick.com/qa/33666/what-are-custom-events-in-google-analytics)**, **async [event handlers](https://answers.mindstick.com/qa/93763/how-to-add-event-handlers-on-html-elements-by-javascript)**, or how events work in **WinForms/WPF**?

## Read More -

- [Overview](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/)
- [How to subscribe to and unsubscribe from events](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-subscribe-to-and-unsubscribe-from-events)
- [How to raise base class events in derived classes](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-raise-base-class-events-in-derived-classes)
- [How to implement interface events](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-implement-interface-events)
- [How to implement custom event accessors](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-implement-custom-event-accessors)

---

Original Source: https://www.mindstick.com/articles/339102/c-sharp-events-a-complete-beginner-s-guide

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
