CHAPTER 23
Beginner
Delegates, Events, and Lambda Expressions
Updated: May 17, 2026
5 min read
# CHAPTER 23
Delegates, Events, and Lambda Expressions
1. Introduction
What if you wanted to pass a *method* as a parameter to another method? What if you want a button click to notify 5 different classes automatically? Delegates and Events form the backbone of C#'s event-driven architecture (essential for UI frameworks like WPF, Unity, and ASP.NET).2. Learning Objectives
By the end of this chapter, you will be able to:- Understand what a Delegate is (a pointer to a function).
-
Use the built-in
ActionandFuncdelegates.
-
Implement publisher-subscriber architecture using
event.
- Understand Lambda Expressions deeply.
3. What is a Delegate?
A Delegate is a type that safely encapsulates a method. Think of it as a variable that holds a method instead of a number or a string.
csharp
4. Action and Func (Modern Delegates)
Declaring custom delegates viapublic delegate... is the old way. Modern C# provides built-in generic delegates to save time:
-
Action<T>: Points to a method that returnsvoid.
-
Func<T, TResult>: Points to a method that returns a value (the last parameter is the return type).
csharp
5. Events (Publisher / Subscriber)
Anevent is a special type of delegate used to broadcast notifications.
- Publisher: The class that triggers (fires) the event.
- Subscriber: The classes that listen for the event.
Imagine an alarm system.
csharp
6. Lambda Expressions
We saw Lambda expressions (=>) in Chapter 21 with LINQ. They are simply anonymous (unnamed) methods written inline. They are heavily used to subscribe to events quickly.
csharp
7. Common Mistakes
-
Using
=instead of+=for Events: If you writeclock.OnRing = WakeUpPerson;, you completely erase all previous subscribers! Always use+=to subscribe and-=to unsubscribe.
-
Memory Leaks with Events: If an object subscribes to a static event but forgets to unsubscribe (
-=) before being destroyed, the event will keep the object alive in memory permanently.
8. Best Practices
-
Never fire an event without checking if it is null first (e.g.,
OnRing?.Invoke();). If an event has no subscribers, calling.Invoke()will throw aNullReferenceException.
9. Exercises
-
1.
Create a delegate
MathOpthat takes two ints and returns an int. Point it to anAddmethod, then reassign it to aMultiplymethod.
-
2.
Rewrite exercise 1 using the built-in
Func<int, int, int>delegate.
10. MCQs with Answers
Question 1
What is a Delegate in C#?
Question 2
Which built-in generic delegate represents a method that returns void?
Question 3
Which built-in generic delegate represents a method that returns a value?
Question 4
In Func<int, string>, what does the method return?
Question 5
What is an Event in C#?
Question 6
Which operator is used to SUBSCRIBE to an event?
Question 7
Which operator is used to UNSUBSCRIBE from an event?
Question 8
What happens if you .Invoke() an event that has zero subscribers?
Question 9
What does the ?. operator do in MyEvent?.Invoke();?
Question 10
Lambda expressions are essentially:
11. Interview Questions
-
Q: Explain the difference between
Action,Func, andPredicate.
- Q: What is the Publisher/Subscriber pattern, and how do C# Events facilitate it?
12. Summary
Delegates treat methods as data, allowing you to pass them around as variables. Modern C# usesAction and Func to simplify this. By layering the event keyword on top of delegates, you can build highly decoupled Publisher-Subscriber architectures, which are the foundation of modern UI and reactive programming.
13. Next Chapter Recommendation
In Chapter 24: Multithreading and Async Programming, we will unlock the full power of the CPU, learning how to run heavy background tasks without freezing the main application usingasync and await.