Lambda Expressions
In this page:
Lambda Expressions
A lambda expression is a compact, inline, unnamed function using the => syntax, useful anywhere a delegate is expected. (x, y) => x + y is equivalent to a full method but written inline. Lambdas are heavily used with LINQ, event handlers, and Func/Action.
Note: A lambda with a single expression body needs no return or braces — the expression's value is returned automatically.
Example: Lambda Expressions
using System;
delegate int Transform(int x);
class Program
{
static void Main(string[] args)
{
Transform square = x => x * x;
Transform increment = x => x + 1;
Console.WriteLine(square(5));
Console.WriteLine(increment(5));
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: