Lambda Expression (C#)
A lambda is a short, anonymous function written inline with the => arrow. It is widely used to pass small bits of behavior to LINQ methods, delegates, and event handlers.
C#
The part before => lists the parameters, and the part after is the body or expression to evaluate. `x => x * 2` takes one input and returns it doubled.
Lambdas can be assigned to Func or Action delegate types, or passed directly to methods like Where, Select, and OrderBy.
Syntax
(parameters) => expressionExamples
C#
Func<int, int> doubleIt = x => x * 2;
Console.WriteLine(doubleIt(5));
int[] nums = { 3, 1, 2 };
var sorted = nums.OrderBy(n => n).ToList();
Console.WriteLine(string.Join(",", sorted));Output:
10
1,2,3