Lambda expressions

C# 3.0 NETFx 3.5

Published Updated Author Jeffrey T. Fritz Reading time

Lambda expressions make delegate-based code shorter and easier to scan, especially when you pass behavior into LINQ, events, and async APIs.

Lambda expressions arrived in C# 3.0. They let you declare small bits of behavior inline, usually with =>, instead of creating a separate named method for every callback.

Why it matters

Lambdas are the glue between data and behavior. They power LINQ queries, event handlers, predicates, projections, and most modern .NET APIs that accept Func<> or Action<>.

They also keep code local to the place where it is used, which helps when the logic is small and the surrounding context matters more than a method name.

Cautions

Use a named method when the logic grows beyond a few lines or needs a clear test target. Lambdas are best for short, focused behavior.

Also remember that lambdas can capture local variables from their surrounding scope. That is powerful, but it can make lifetime and mutation behavior less obvious if you overuse it.

Replacing anonymous methods with a lambda

Lambda expressions let you write compact delegates inline without losing type safety.

Valid since C# 3.0

Func<int, int> doubleIt = value => value * 2;

Learn more

Lambda expressions (Microsoft Learn)