What are delegates in C# and how do they enable callbacks?
Understand delegates in C#: type-safe method references that power callbacks, events, and LINQ. See multicast delegates, Action/Func, and code examples.
Expected Interview Answer
A delegate is a type-safe reference to a method with a specific signature, so you can store, pass, and invoke methods like data. This is what enables callbacks: you hand a delegate to another method and it calls you back through it when the right moment arrives.
Declaring a delegate defines a signature; an instance can then point to any matching method, whether static, instance, lambda, or anonymous. Because a delegate is a first-class value, a method can accept one as a parameter and invoke it later, inverting control so the callee triggers your code. Delegates are also multicast, chaining several methods on one invocation, and they underpin events, LINQ, and asynchronous callbacks in .NET.
- Type-safe method references checked at compile time
- Enable callbacks and inversion of control
- Support multicast invocation of many methods
- Foundation for events, LINQ, and async patterns
- Allow flexible, pluggable behaviour without subclassing
AI Mentor Explanation
A delegate is like handing the third umpire your phone number before a match: you give the on-field team a way to reach a specific reviewer whose job matches a fixed brief. When a close run-out happens, they don't decide themselves — they call the number you supplied and the reviewer runs their routine. That callback is the delegate being invoked later, at the moment the game demands it.
Step-by-Step Explanation
Step 1
Declare the delegate type
Define a delegate with a signature, e.g. public delegate void Notify(string message), or reuse Action/Func.
Step 2
Write a matching method
Create any static or instance method whose parameters and return type match the delegate signature.
Step 3
Create a delegate instance
Assign a compatible method, lambda, or anonymous method to a variable of the delegate type.
Step 4
Pass it as a callback
Give the delegate to another method as a parameter so that method holds a reference to your code.
Step 5
Invoke to call back
The receiving method invokes the delegate at the right time, executing your method — the callback.
What Interviewer Expects
- That a delegate is a type-safe method reference
- How passing a delegate enables inversion of control
- Awareness of Action, Func, and Predicate built-ins
- Understanding of multicast delegate chaining
- Link between delegates and events, LINQ, and async
Common Mistakes
- Confusing a delegate type with a delegate instance
- Thinking delegates are not type-safe
- Forgetting to null-check before invoking (or use ?.Invoke)
- Assuming a delegate can point to only one method
- Not knowing Action and Func are generic delegates
Best Answer (HR Friendly)
“A delegate is a variable that holds a method instead of a value, so you can pass a piece of code around like data. That lets one method accept your method and call it back later when something happens, which is how callbacks and event handlers work in C#.”
Code Example
public delegate void ProgressCallback(int percent);
public class Downloader
{
public void Run(ProgressCallback onProgress)
{
for (int i = 0; i <= 100; i += 50)
onProgress?.Invoke(i); // call back into caller's code
}
}
// usage
var d = new Downloader();
d.Run(p => Console.WriteLine($"Progress: {p}%"));
// built-in delegate equivalents
Action<int> log = p => Console.WriteLine(p);
Func<int, int> square = x => x * x;Follow-up Questions
- What is the difference between Action, Func, and Predicate?
- How do multicast delegates handle return values?
- How are delegates related to events in C#?
- What is a lambda expression and how does it map to a delegate?
- How does the null-conditional Invoke avoid exceptions?
MCQ Practice
1. A delegate in C# is best described as?
A delegate is a type that safely references methods with a matching signature, allowing methods to be stored and invoked like data.
2. Which built-in generic delegate returns a value?
Func<...,TResult> returns a value, while Action returns void.
3. What does invoking a multicast delegate do?
A multicast delegate invokes every method in its invocation list in the order they were added.
Flash Cards
What is a delegate? — A type-safe reference to a method with a specific signature.
How do delegates enable callbacks? — You pass a delegate to a method, which invokes it later to call your code back.
Action vs Func? — Action returns void; Func returns a value.
What is a multicast delegate? — A delegate whose invocation list holds several methods, all called on invoke.
Safe way to invoke? — Use delegate?.Invoke(args) to avoid a null reference exception.