C# Interview Question #56
What is a lambda expression in C#?
Exceptions, Delegates, Events & Lambdas Mid-Level Intermediate
Quick Interview Answer
A lambda expression is a concise way to represent an anonymous function. Lambdas are commonly used with delegates, LINQ, events, and asynchronous APIs.
Detailed Explanation
A lambda expression is a concise way to represent an anonymous function. Lambdas are commonly used with delegates, LINQ, events, and asynchronous APIs.
The lambda operator is =>. Parameters appear on the left and the expression or statement body appears on the right.
Lambdas can capture variables from their surrounding scope, which creates a closure. Captures should be understood in performance-sensitive or long-lived code because they can affect allocations and object lifetimes.
Code Example
Func<int, int> square = x => x * x;
var active = products
.Where(p => p.IsActive)
.ToList();