A lambda creates an anonymous function: a function whose name is unnecessary. Lambdas are useful for one-off callbacks, such as handling command-line parsing failures, without adding a named method. section 2.8
C# has two lambda forms:
- An expression lambda uses a single expression or statement and needs no braces. section 2.8
- A statement lambda uses multiple statements inside braces. section 2.8
- Lambdas can be assigned to Action or Func delegates. Their generic arguments let the compiler infer parameter and return types. section 2.8
Listing 2.19 Examples of lambda expression types
Action<int, int> a1 = (x, y) => WriteLine(x + "," + y); #1
Action<string, string> a2 = (left, right) => #2
{
if (left == right)
{
WriteLine("Matches!");
}
else
{
WriteLine("Does not match!");
}
};
Func<int, int, int> multiply = (x, y) => x * y; #3
Func<IEnumerable<int>, int> sum = #4
numbers => #5
{
int running = 0;
foreach (int num in numbers)
{
running += num;
}
return running;
};
var parse = (string s) => int.Parse(s); #6
Examples range from concise expression lambdas to multi-statement bodies, including callbacks that print, compare strings, multiply, sum, and parse. section 2.8

















