Delegates
C# / .NET Interview Notes
1. What are Delegates?
A delegate is a type-safe reference to a method.
It allows a method to be:
- Stored in a variable
- Passed as a parameter
- Returned from another method
- Invoked later
Variable → Stores a value
Delegate → Stores a reference to a method
⭐ Key Idea: A delegate allows behavior to be treated like a value and passed around in a type-safe way.
2. Basic Delegate
delegate void MyDelegate();
void SayHello() => Console.WriteLine("Hello");
MyDelegate action = SayHello;
action(); // Hello
3. Delegate Signature
| Delegate | Parameters | Return |
|---|---|---|
delegate void MyDelegate(); | None | void |
delegate void MsgDel(string msg); | string | void |
delegate int MathOp(int a, int b); | int, int | int |
4. Why Use Delegates?
delegate int MathOperation(int a, int b);
MathOperation op = Add;
Console.WriteLine(op(10, 20)); // 30
op = Multiply;
Console.WriteLine(op(10, 20)); // 200
5. Passing a Delegate to a Method
void Execute(MathOperation operation)
{
int result = operation(10, 20);
Console.WriteLine(result);
}
Execute(Add); // 30
Execute(Multiply); // 200
6. Anonymous Methods
MathOperation operation = delegate (int a, int b)
{
return a + b;
};
Console.WriteLine(operation(10, 20)); // 30
7. Lambda Expressions
MathOperation operation = (a, b) => a + b;
MathOperation multiply = (a, b) => a * b;
⭐ Important: Delegate defines the signature. Lambda provides the implementation.
8. Built-in Delegate — Action
Action<string> print = message => Console.WriteLine(message);
print("Hello");
Action<int, int> printSum = (a, b) => Console.WriteLine(a + b);
9. Built-in Delegate — Func
Func<int, int, int> add = (a, b) => a + b;
int result = add(10, 20);
Func<string, int> getLength = text => text.Length;
In Func<int, int, int>:
- The first two
intparameters are the inputs - The last
intis the return type
10. Built-in Delegate — Predicate<T>
Predicate<int> isEven = number => number % 2 == 0;
bool result = isEven(10); // true
11. Action vs. Func vs. Predicate
| Delegate | Input | Return |
|---|---|---|
Action | Zero or more parameters | void |
Func | Zero or more parameters | Value |
Predicate<T> | One T | bool |
⭐ Memory Trick:
Action → DO something (void)
Func → GIVE something (returns value)
Predicate → ANSWER yes/no (bool)
12. Delegates in LINQ
var numbers = new List<int">> { 1, 2, 3, 4, 5, 6 };
var evenNumbers = numbers.Where(x => x % 2 == 0);
// x => x % 2 == 0 is a lambda passed to Where()
13. Delegates and Events
Button
↓
Click Event
↓
Contains method references
↓
Button_Click()
14. Multicast Delegates
Action action;
action = Method1;
action += Method2;
action += Method3;
action(); // Invokes Method1, Method2, Method3
action -= Method2; // Remove Method2
⭐
+= adds a method. -= removes a method.
15. Real-World Example — Notifications
Action<string> notify;
notify += SendEmail;
notify += SendSms;
notify += SendPushNotification;
notify("Order completed");
// All three methods are called
16. Delegate vs. Method
Method → Actual behavior
Delegate → Reference to compatible behavior
17. Delegate vs. Interface
Delegate → "Here is a method/behavior."
Interface → "Here is an object that follows this contract."
18. Type Safety
Delegates are type-safe. The compiler checks whether the method's parameters and return type are compatible with the delegate signature.
19. Callback Pattern
void Process(Action callback)
{
Console.WriteLine("Processing...");
callback();
}
Process(() => Console.WriteLine("Done!"));
20. Interview Takeaways ⭐
What is a delegate?
A delegate is a type-safe reference to a method.
Why are delegates useful?
They allow behavior to be passed around independently — useful for callbacks, events, LINQ.
Action
Returns void.
Func
Returns a value. Last generic param is the return type.
Predicate
Accepts T, returns bool.
Multicast Delegate
References multiple methods. += adds, -= removes.
Are delegates type-safe?
Yes. The compiler checks compatibility.
21. Quick Revision
| Concept | Remember |
|---|---|
| Delegate | Type-safe reference to a method |
| Action | Returns void |
| Func | Returns a value |
| Predicate | Returns bool |
| Lambda | Concise way to define function behavior |
| Multicast | Delegate referencing multiple methods |
| Callback | Passing behavior for later invocation |
| Events | Built around delegate-based invocation |
22. One-Line Interview Answer ⭐
A delegate in C# is a type-safe method reference that allows behavior to be passed around as a value, enabling callbacks, events, LINQ operations, and flexible method execution.