---
title: "What is a delegate? How is it different from an event?"  
description: "What is a delegate? How is it different from an event?"  
author: "Ravi Vishwakarma"  
published: 2025-07-01  
updated: 2026-05-28  
canonical: https://www.mindstick.com/forum/161764/what-is-a-delegate-how-is-it-different-from-an-event  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# What is a delegate? How is it different from an event?

**What is a [delegate](https://www.mindstick.com/articles/777/delegate-and-event-in-c-sharp)? How is it different from an [event](https://www.mindstick.com/articles/167719/marquee-hire-benefits-of-event-lighting-hire-london)?**

## Replies

### Reply by Anubhav Sharma

In languages like C#, both **delegates** and **events** are used for handling methods dynamically and enabling communication between objects. They are closely related, but they serve different purposes.

## What is a Delegate?

A **delegate** is a type-safe function pointer.

It stores a reference to a method and allows you to call that method indirectly.

You can think of a delegate as:

> “A variable that can hold a method.”

## Delegate Syntax

```plaintext
// Declare delegate
public delegate void MessageDelegate(string message);
```

This delegate can store any method that:

Returns `void`

Accepts a `string` parameter

## Delegate Example

```cs
// Import namespace
using System;

class Program
{
    // Create delegate
    public delegate void PrintDelegate(string text);

    // Method matching delegate signature
    static void PrintMessage(string message)
    {
        // Print message
        Console.WriteLine(message);
    }

    static void Main()
    {
        // Store method inside delegate
        PrintDelegate pd = PrintMessage;

        // Invoke method using delegate
        pd("Hello Delegate");
    }
}
```

Output:

```plaintext
Hello Delegate
```

## What is an Event?

An **event** is a wrapper around a delegate that provides controlled access.

Events are used for:

- Notifications
- Callbacks
- Observer pattern
- UI interactions

Example:

- Button click
- Mouse movement
- File download completed

## Event Syntax

```plaintext
// Declare event
public event MessageDelegate OnMessage;
```

Here:

- `MessageDelegate` is the delegate type
- `OnMessage` is the event

## Event Example

```cs
// Import namespace
using System;

class Publisher
{
    // Declare delegate
    public delegate void Notify(string message);

    // Declare event
    public event Notify OnNotify;

    // Trigger event
    public void RaiseEvent()
    {
        // Check if event has subscribers
        if (OnNotify != null)
        {
            // Fire event
            OnNotify("Event Triggered");
        }
    }
}

class Program
{
    // Event handler method
    static void ShowMessage(string msg)
    {
        // Print message
        Console.WriteLine(msg);
    }

    static void Main()
    {
        // Create publisher object
        Publisher pub = new Publisher();

        // Subscribe to event
        pub.OnNotify += ShowMessage;

        // Trigger event
        pub.RaiseEvent();
    }
}
```

Output:

```plaintext
Event Triggered
```

## Key Difference Between Delegate and Event

| Feature | Delegate | Event |
| --- | --- | --- |
| Purpose | Store/call methods | Notify subscribers |
| Access | Can be invoked directly | Can only be invoked inside declaring class |
| Usage | Method references | Publisher-subscriber pattern |
| Encapsulation | Less secure | More secure |
| Operators | Assignment allowed | Only `+=` and `-=` outside class |
| Typical Use | Callbacks, strategy pattern | UI events, notifications |

## Important Concept

A delegate is like:

> “Holding a phone number.”

An event is like:

> “Subscribing to a notification service.”

## Why Events are Safer

With delegates:

```cs
// Dangerous: external code can overwrite delegate
myDelegate = null;
```

With events:

```cs
// Only subscription allowed
myEvent += Handler;
```

External classes cannot directly trigger or reset the event.

Only the owner class can invoke it.

## Relationship Between Delegate and Event

Events are built on top of delegates.

Internally:

```cs
public event Notify OnNotify;
```

uses a delegate behind the scenes.

So:

Delegate = foundation

Event = controlled notification mechanism

## Real-World Example

## Delegate

A delegate is like:

- Saving a contact number
- Calling the contact anytime

## Event

An event is like:

- You subscribe to YouTube notifications
- Creator uploads video
- You receive notification automatically

## Built-in Delegates in C#

C# already provides common delegates:

## Action

```cs
// No return value
Action<string> action = PrintMessage;
```

## Func

```cs
// Returns value
Func<int, int, int> add = (a, b) => a + b;
```

## Predicate

```cs
// Returns bool
Predicate<int> isEven = x => x % 2 == 0;
```

## When to Use Delegate

Use delegates when:

- Passing methods as parameters
- Creating callbacks
- Implementing strategy pattern

## When to Use Event

Use events when:

- One object should notify many objects
- Building UI applications
- Creating observer-based systems
- Handling asynchronous notifications

## Summary

## Delegate

- Stores method references
- Can invoke methods dynamically
- Similar to function pointers

## Event

- Built on delegates
- Provides controlled notifications
- Supports publisher-subscriber pattern

Most real-world C# applications use events extensively for communication between components.


---

Original Source: https://www.mindstick.com/forum/161764/what-is-a-delegate-how-is-it-different-from-an-event

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
