What are lambda expressions and how do they relate to delegates in C#?
Learn how C# lambda expressions relate to delegates: the => operator, Func and Action, closures and expression trees, with clear code examples.
Expected Interview Answer
A lambda expression is a concise, inline anonymous function written with the => operator, and it is C#'s shorthand for creating a delegate or expression tree instance without declaring a named method.
A delegate is a type-safe reference to a method with a specific signature; a lambda supplies the method body inline and the compiler converts it into an instance of a compatible delegate type such as Action, Func, or Predicate. Lambdas can capture variables from the enclosing scope (closures) and, when assigned to Expression<TDelegate>, become expression trees that libraries like LINQ-to-SQL translate into other forms. So lambdas do not replace delegates — they are the most convenient way to produce delegate instances.
- Less boilerplate than named methods or anonymous methods
- Enables fluent LINQ query syntax
- Supports closures that capture surrounding variables
- Can compile to expression trees for query translation
- Improves readability for short callbacks and event handlers
AI Mentor Explanation
A delegate is like the role of 'the next batter to face this over' — it defines a slot with fixed responsibilities. A lambda is you quickly nominating a player on the spot to fill that slot instead of formally listing them in the team sheet beforehand. The role stays the same; the lambda just names who plays it inline, without paperwork.
Step-by-Step Explanation
Step 1
Define the delegate type
Choose or declare a delegate signature, e.g. Func<int, int, int> for a method taking two ints and returning an int.
Step 2
Write the lambda
Use parameters => expression or parameters => { statements } to supply the method body inline.
Step 3
Assign to the delegate
The compiler converts the lambda into an instance of the compatible delegate type.
Step 4
Capture variables if needed
Reference outer variables inside the lambda; the compiler generates a closure to hold them.
Step 5
Invoke it
Call the delegate like a method, passing arguments; the lambda body runs.
What Interviewer Expects
- Clear definition of lambda vs delegate vs anonymous method
- Knowledge of Func, Action and Predicate built-in delegates
- Understanding of closures and captured variables
- Awareness of expression trees via Expression<TDelegate>
- A correct code example using the => operator
Common Mistakes
- Saying a lambda is a delegate rather than a way to create one
- Confusing statement lambdas with expression lambdas
- Ignoring variable capture and closure lifetime issues
- Not knowing the difference between Func and Action
- Assuming every lambda becomes an expression tree
Best Answer (HR Friendly)
“A lambda expression is a short way to write a small function directly where you need it, using the => arrow. In C# it is the everyday way to fill a delegate, which is basically a variable that points to a method, so you can pass behaviour around like data.”
Code Example
// Func delegate: two ints in, one int out
Func<int, int, int> add = (a, b) => a + b;
Console.WriteLine(add(3, 4)); // 7
// Action delegate: takes input, returns nothing
Action<string> greet = name => Console.WriteLine($"Hi {name}");
greet("Sam");
// Predicate used in LINQ; captures 'min' as a closure
int min = 10;
var numbers = new[] { 5, 12, 8, 20 };
var big = numbers.Where(n => n > min); // 12, 20Follow-up Questions
- What is the difference between Func, Action and Predicate?
- What is a closure and when can captured variables cause bugs?
- How does Expression<TDelegate> differ from a compiled lambda?
- When would you still declare a named delegate type?
- How do lambdas enable LINQ method syntax?
MCQ Practice
1. What does a lambda expression create when assigned to a Func variable?
The compiler converts the lambda into an instance of the compatible delegate type, here a Func.
2. Which built-in delegate returns no value?
Action represents a method that takes parameters but returns void; Func and Predicate return values.
3. A lambda referencing an outer local variable creates a?
Capturing an enclosing variable causes the compiler to generate a closure that keeps that variable alive.
Flash Cards
What operator defines a lambda? — The => (goes-to) operator, separating parameters from the body.
Delegate vs lambda? — A delegate is a method-reference type; a lambda is the inline way to create a delegate instance.
What is a closure? — A lambda that captures and keeps alive variables from its enclosing scope.
Func vs Action? — Func returns a value; Action returns void.
Expression<Func<...>>? — An expression tree — a data representation of the lambda that can be translated, e.g. to SQL.