Syntax
Context: Lambda syntax: (parameters) => expression. The => is the lambda operator.
No parameters
Section titled “No parameters”Action greet = () => Console.WriteLine("Hello");One parameter (parentheses optional)
Section titled “One parameter (parentheses optional)”Func<int, int> doubleIt = x => x * 2;Multiple parameters
Section titled “Multiple parameters”Func<int, int, int> add = (a, b) => a + b;Explicitly typed parameters
Section titled “Explicitly typed parameters”Func<int, string> toString = (int x) => x.ToString();Real-world usage example
Section titled “Real-world usage example”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").