Skip to content

Syntax

Context: Lambda syntax: (parameters) => expression. The => is the lambda operator.

Action greet = () => Console.WriteLine("Hello");
Func<int, int> doubleIt = x => x * 2;
Func<int, int, int> add = (a, b) => a + b;
Func<int, string> toString = (int x) => x.ToString();

Event handlers: Use lambda for simple event handling without a separate method.

button.Click += (sender, e) => Console.WriteLine("Clicked");

Example: In ASP.NET Core Minimal APIs, lambdas are used as endpoint handlers: app.MapGet("/", () => "Hello").