What is the difference between an event and a delegate in C#?
Learn the difference between events and delegates in C#: how events restrict a delegate to safe subscribe/unsubscribe access for the publish-subscribe pattern.
Expected Interview Answer
A delegate is a type that references methods, while an event is a restricted wrapper around a delegate that lets outside code only subscribe or unsubscribe, never invoke or overwrite the invocation list. In short, an event is a delegate exposed safely for the publish-subscribe pattern.
A public delegate field lets any consumer reassign it with =, clear other subscribers, or raise it directly, which is dangerous. The event keyword permits only += and -= from outside the declaring class, and only the owner can raise it. This encapsulation is why events model notifications correctly: the publisher controls when the event fires, and subscribers merely register handlers.
- Encapsulates the delegate's invocation list
- Only the declaring class can raise the event
- Subscribers limited to += and -= safely
- Prevents accidental overwrite of all handlers
- Standardizes the publish-subscribe pattern
AI Mentor Explanation
A delegate is like the open team group chat where anyone can post, delete others' messages, or blast an announcement to everyone. An event is like the official scoreboard feed: fans may subscribe or unsubscribe to updates, but only the match scorer can actually push a new score. The subscribe-only access protects the feed from being hijacked or wiped by a random spectator.
Step-by-Step Explanation
Step 1
Define the delegate type
Declare a delegate signature, or use the standard EventHandler / EventHandler<T> for events.
Step 2
Declare the event
In the publisher class, declare a field with the event keyword based on that delegate type.
Step 3
Subscribe from outside
Consumers attach handlers with += and detach with -=; they cannot assign, clear, or raise it.
Step 4
Raise inside the owner
Only the declaring class invokes the event, typically via a protected OnXxx method with a null-conditional call.
Step 5
Handlers run
When raised, every subscribed handler in the invocation list executes, receiving the event arguments.
What Interviewer Expects
- That an event wraps and restricts a delegate
- Only += / -= allowed from outside the class
- Only the owner can raise the event
- Why encapsulation matters for publish-subscribe
- Familiarity with EventHandler and the OnXxx raise pattern
Common Mistakes
- Saying events and delegates are identical
- Claiming external code can invoke or reassign an event
- Exposing a public delegate field instead of an event
- Forgetting a null check before raising the event
- Not knowing the standard EventHandler signature
Best Answer (HR Friendly)
“A delegate is a variable that can hold and call methods, and anyone with access can overwrite or fire it. An event is a safer version built on a delegate: outsiders can only add or remove their own handlers, and only the class that owns the event is allowed to fire it.”
Code Example
public class Button
{
// event: subscribers can only += / -=
public event EventHandler? Clicked;
protected virtual void OnClicked()
=> Clicked?.Invoke(this, EventArgs.Empty); // only owner raises
public void SimulatePress() => OnClicked();
}
// usage
var btn = new Button();
btn.Clicked += (s, e) => Console.WriteLine("Clicked!");
btn.SimulatePress();
// btn.Clicked = null; // compile error from outside
// btn.Clicked.Invoke(...); // not allowed outside the classFollow-up Questions
- Why can't external code invoke an event directly?
- What is the standard EventHandler<TEventArgs> pattern?
- How do you safely raise an event to avoid null and race conditions?
- What are custom event accessors (add/remove)?
- How does the observer pattern relate to C# events?
MCQ Practice
1. From outside the declaring class, what can you do with an event?
Outside the declaring class, an event permits only subscribing (+=) and unsubscribing (-=); it cannot be invoked or reassigned.
2. Who is allowed to raise (invoke) an event?
Only code inside the class that declares the event can invoke it, giving the publisher control over when it fires.
3. An event is essentially?
The event keyword wraps a delegate and restricts external access to subscription operations only.
Flash Cards
What is an event in C#? — A restricted wrapper around a delegate allowing only subscribe/unsubscribe from outside.
Who can raise an event? — Only the class that declares it.
What operations can external code do on an event? — Only += and -=.
Why prefer an event over a public delegate? — It prevents outsiders from overwriting or invoking the invocation list.
Standard event delegate type? — EventHandler or EventHandler<TEventArgs>.