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!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: