Read the syntax
A lambda uses => to define a function-like value. The compiler infers parameter types when a target delegate type such as Func<int,int> provides them.
Expression-bodied lambdas return their expression; a block body needs return for a non-void delegate. Lambdas can be passed to Sort, LINQ and other APIs that accept behavior.
Syntax
(parameters) => expressionExamples
Basic Lambda Syntax
Comparing a lambda to the equivalent named method it replaces.
// Old way: a named method
int SquareMethod(int x) => x * x;
// New way: a lambda assigned to a Func delegate
Func<int, int> square = x => x * x;
Console.WriteLine(square(5)); // 25
Console.WriteLine(SquareMethod(5)); // 25- Comparing a lambda to the equivalent named method it replaces.
- A lambda uses => to define a function-like value. The compiler infers parameter types when a target delegate type such as Func<int,int> provides them.
- Both the local SquareMethod and the Func lambda multiply x by itself. Passing 5 to each prints 25 twice.
Func, Action, and Predicate
The three most common built-in delegate types used with lambdas.
Func<int, int, int> add = (a, b) => a + b; // takes 2 ints, returns an int
Console.WriteLine(add(3, 4)); // 7
Action<string> print = message => Console.WriteLine($"Log: {message}");
print("Hello"); // takes a string, returns nothing
Predicate<int> isEven = n => n % 2 == 0;
Console.WriteLine(isEven(4)); // true // takes a value, returns bool- The three most common built-in delegate types used with lambdas.
- A lambda uses => to define a function-like value. The compiler infers parameter types when a target delegate type such as Func<int,int> provides them.
- Func returns 7 from addition, Action writes a log line without a return value, and Predicate reports True because 4 is even.
Multi-Statement Lambdas
A lambda body can contain multiple statements using curly braces and an explicit return.
Func<int, string> classify = n =>
{
if (n < 0) return "negative";
else if (n == 0) return "zero";
else return "positive";
};
Console.WriteLine(classify(-5)); // negative
Console.WriteLine(classify(0)); // zero- A lambda body can contain multiple statements using curly braces and an explicit return.
- A lambda uses => to define a function-like value. The compiler infers parameter types when a target delegate type such as Func<int,int> provides them.
- The block lambda checks negative, zero and positive cases in order. Calls with -5 and 0 print negative and zero.
Lambdas with Sorting and Collections
A very common use: passing a lambda to control sort order or filter a collection.
List<string> names = new List<string> { "Charlie", "Alice", "Bob" };
names.Sort((a, b) => a.CompareTo(b));
Console.WriteLine(string.Join(", ", names)); // Alice, Bob, Charlie
names.Sort((a, b) => b.Length - a.Length); // longest name first
Console.WriteLine(string.Join(", ", names));- A very common use: passing a lambda to control sort order or filter a collection.
- A lambda uses => to define a function-like value. The compiler infers parameter types when a target delegate type such as Func<int,int> provides them.
- The first comparison sorts names alphabetically. The second compares lengths in reverse so longer names appear first; equal-length ordering is not specified by this comparison.
Best practices
- Use lambdas for short, simple implementations passed as arguments - switch to a full local function or method once the logic gets long or complex
- Use Func<> when the lambda returns a value, Action<> when it does not, and Predicate<> specifically for a single bool-returning test
- Keep lambda parameter names short but meaningful, especially in LINQ chains where context is often clear from position
- Remember lambdas capture variables from their enclosing scope (closures) - be mindful of this when a lambda outlives the loop iteration that created it
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference