C# Feature – Lambda expressions
Overview
Section titled “Overview”Lambdas are anonymous functions with expression or block bodies, foundational to LINQ and functional patterns.
Introduced In
Section titled “Introduced In”C# 3 (2007)
Before
Section titled “Before”List<int> Filter(List<int> xs, Predicate<int> pred) { /* ... */ }
var evens = numbers.Where(n => n % 2 == 0).ToList();
Annotated Example
Section titled “Annotated Example”Func<int,int> square = x => x * x; // expression-bodiedConsole.WriteLine(square(5));
Gotchas & Best Practices
Section titled “Gotchas & Best Practices”- Capture semantics: avoid capturing loop variables incorrectly.
Related Features
Section titled “Related Features”Version Notes
Section titled “Version Notes”- Default lambda parameters in C# 12.