What is a Lambda Expression in C#?
๐ก Concept: Lambda Expression
A lambda expression in C# is an anonymous function that provides a concise syntax to write inline code blocks or expressions. It is widely used with delegates and LINQ to create compact, readable code.
๐ Quick Intro
Lambda expressions use the =>
syntax and can be expressions or statement blocks. They simplify delegate instantiation and are fundamental to functional programming in C#.
๐ง Analogy
Imagine lambda expressions as shorthand notes. Instead of writing a long letter (full method), you jot a quick note that gets the message across efficiently and clearly.
๐ง Technical Explanation
- โก Lambda expressions provide inline anonymous methods.
- ๐ They are syntactic sugar for delegates and expression trees.
- ๐ฏ Widely used in LINQ for querying collections.
- ๐ Can be expression lambdas (single expression) or statement lambdas (multiple statements).
- ๐ Support closures, capturing variables from the outer scope.
๐ฏ Use Cases
- โ Simplifying delegate and event handlers.
- โ Writing LINQ queries for filtering, projection, and aggregation.
- โ Creating inline functions without declaring separate methods.
- โ Functional programming constructs and asynchronous programming.
๐ป Code Example
// Lambda expression for delegate
Func<int, int, int> add = (x, y) => x + y;
Console.WriteLine(add(5, 3)); // Outputs 8
// Lambda in LINQ
int[] numbers = {1, 2, 3, 4, 5};
var evens = numbers.Where(n => n % 2 == 0);
foreach(var num in evens) {
Console.WriteLine(num);
}

โ Interview Q&A
Q1: What is a lambda expression?
A: It's an anonymous inline function using the => syntax.
Q2: How are lambda expressions related to delegates?
A: Lambdas are compiled to delegate instances or expression trees.
Q3: Can lambda expressions capture outer variables?
A: Yes, they support closures.
Q4: What is the difference between expression and statement lambdas?
A: Expression lambdas have a single expression; statement lambdas can have multiple statements with braces.
Q5: Where are lambda expressions mostly used?
A: In LINQ queries and event handlers.
Q6: Can lambda expressions be async?
A: Yes, by using the async keyword.
Q7: Are lambda expressions type-safe?
A: Yes, they are strongly typed.
Q8: What keyword is used to define a lambda?
A: The => operator.
Q9: Can lambdas replace anonymous methods?
A: Yes, lambdas are preferred in modern C#.
Q10: Can you assign lambdas to variables?
A: Yes, typically to delegates or Func/Action types.
๐ MCQs
Q1. What syntax is used to define a lambda expression?
- ->
- =>
- ::
- ->>
Q2. What keyword is commonly used with lambdas in LINQ?
- Select
- Where
- Join
- GroupBy
Q3. Can lambda expressions access variables from outer scope?
- No
- Yes
- Only static
- Only const
Q4. Are lambdas type-safe?
- No
- Yes
- Only in .NET 5+
- Only with var
Q5. What is an expression lambda?
- A lambda with multiple statements
- A lambda with a single expression
- A named method
- An event handler
Q6. Can lambdas be async?
- No
- Yes
- Only in C# 9+
- Only delegates
Q7. Which delegate types are commonly used with lambdas?
- Func and Action
- EventHandler
- Predicate
- Delegate
Q8. What operator separates parameters and body in lambda?
- ->
- =>
- ::
- >>
Q9. Can lambdas replace anonymous methods?
- No
- Yes
- Sometimes
- Rarely
Q10. Where are lambdas extensively used?
- Reflection
- LINQ
- Async/Await
- Delegates
๐ก Bonus Insight
Lambda expressions empower C# developers to write elegant, concise, and expressive code, especially in LINQ and asynchronous programming. Mastering lambdas unlocks modern C# programming patterns.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!