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
// Declare delegate
public delegate void MessageDelegate(string message);
This delegate can store any method that:
Returns void
Accepts a string parameter
Delegate Example
// 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:
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
// Declare event
public event MessageDelegate OnMessage;
Here:
MessageDelegate is the delegate type
OnMessage is the event
Event Example
// 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:
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:
// Dangerous: external code can overwrite delegate
myDelegate = null;
With events:
// 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:
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
// No return value
Action<string> action = PrintMessage;
Func
// Returns value
Func<int, int, int> add = (a, b) => a + b;
Predicate
// 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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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:
Delegate Syntax
This delegate can store any method that:
Returns
voidAccepts a
stringparameterDelegate Example
Output:
What is an Event?
An event is a wrapper around a delegate that provides controlled access.
Events are used for:
Example:
Event Syntax
Here:
MessageDelegateis the delegate typeOnMessageis the eventEvent Example
Output:
Key Difference Between Delegate and Event
+=and-=outside classImportant Concept
A delegate is like:
An event is like:
Why Events are Safer
With delegates:
With events:
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:
uses a delegate behind the scenes.
So:
Delegate = foundation
Event = controlled notification mechanism
Real-World Example
Delegate
A delegate is like:
Event
An event is like:
Built-in Delegates in C#
C# already provides common delegates:
Action
Func
Predicate
When to Use Delegate
Use delegates when:
When to Use Event
Use events when:
Summary
Delegate
Event
Most real-world C# applications use events extensively for communication between components.