Difference between delegate and event in C#
By FoxLearn 2/7/2025 3:52:05 AM 55
C# Delegate
A delegate is a type-safe function pointer that holds references to one or more methods. It allows you to call methods dynamically at runtime.
Declared using the delegate
keyword.
public delegate void Notify();
It can hold references to one or more methods that match its signature. A delegate is essentially a method reference.
You can assign, remove, or modify the methods directly.
Notify myDelegate = MyMethod; myDelegate += AnotherMethod; // Adds a new method to the delegate myDelegate(); // Calls both MyMethod and AnotherMethod
You can assign a single method or combine methods directly using the =
and +=
operators.
myDelegate = MyMethod; // Valid myDelegate += AnotherMethod; // Valid
Delegates are independent and do not rely on events. You can use a delegate on its own without needing an event.
C# Event
An event in C# is a higher-level abstraction built on top of delegates, specifically used for notifications and event-driven programming. It restricts the external manipulation of the delegate's invocation list.
Declared using the event
keyword.
public event Notify MyEvent;
It acts as a mechanism for notifying multiple subscribers when something happens (such as a button click, data change, etc.).
An event cannot exist without a delegate.
The event is essentially a wrapper around the delegate to limit how it can be used.
- You can only add or remove event handlers using
+=
or-=
(i.e., theAdd
andRemove
methods). - Direct assignment with
=
is not allowed.
Events provide encapsulation, allowing only the class that declares the event to raise the event. External code can only subscribe or unsubscribe to the event.
MyEvent += MyEventHandler; // Valid MyEvent = MyEventHandler; // Error
The event ensures that external code cannot overwrite the event handler list, providing better control and preventing accidental removal or overwriting of event handlers.
Differences Between Delegate and Event
The following table outlines the differences between a delegate and an event in C#:
Delegate | Event |
---|---|
A delegate is declared using the delegate keyword. | An event is declared using the event keyword. |
A delegate is essentially a function pointer, holding references to one or more methods at runtime. | An event is a mechanism for notifying subscribers, which relies on delegates. |
A delegate is independent and can be used without an event. | An event is tightly coupled with a delegate and cannot exist without one. It serves as a wrapper to prevent external manipulation of the delegate, only allowing methods to be added or removed from the invocation list. |
A delegate provides Combine() and Remove() methods to add or remove methods from its invocation list. | The EventInfo class provides the ability to inspect events and hook up event handlers, with methods like AddEventHandler() and RemoveEventHandler() to add and remove methods. |
A delegate can be passed as a parameter to methods. | An event cannot be passed as a method parameter but can be raised. |
The = operator is used to assign a single method to a delegate, while the += operator is used to assign multiple methods to it. | The = operator cannot be used with events, only += and -= can add or remove event handlers, and these internally call AddEventHandler() and RemoveEventHandler() . |
A delegate allows direct assignment and invocation, while an event restricts direct assignment and focuses on managing the invocation list. | Even though an event is built on delegates, it provides better control by disallowing direct assignment and making the invocation list |
- Using the OrderBy and OrderByDescending in LINQ
- Querying with LINQ
- Optimizing Performance with Compiled Queries in LINQ
- MinBy() and MaxBy() Extension Methods in .NET
- SortBy, FilterBy, and CombineBy in NET 9
- Exploring Hybrid Caching in .NET 9.0
- Using Entity Framework with IDbContext in .NET 9.0
- Primitive types in C#