Lambda Expression in C# (With Examples)
Learn what lambda expressions are in C#, their syntax, and how they simplify writing concise, functional-style code with practical examples.
What is a Lambda Expression?
A lambda expression is a concise way to represent an anonymous method using the => (goes to) operator. It allows developers to write inline functions without formally defining a separate named method.
Basic Syntax
Lambda expressions follow the pattern (parameters) => expression or (parameters) => { statements }, letting you pass small pieces of logic directly wherever a delegate or function type is expected.
Using Lambdas with LINQ
Lambda expressions shine when combined with LINQ, enabling concise queries like list.Where(x => x > 10) that filter, transform, and aggregate collections in just a single readable line of code.
Lambdas with Delegates and Events
Beyond LINQ, lambda expressions are commonly used with delegates like Func and Action, as well as for wiring up event handlers quickly without cluttering the code with extra named methods.
.png)