using System;
using System.Collections.Generic;
class Program {
static void Show(IEnumerable<int> items) {
foreach (int n in items) Console.Write(n + " ");
Console.WriteLine();
}
static void Main() {
List<int> numbers = new List<int>(new int[] { 1, 4, 6, 9 });
// *** before: ***
// an anonymous method of C# 2.0 still repeated the keyword and the
// type of every parameter
Show(numbers.FindAll(delegate(int n) { return n > 5; }));
numbers.Sort(delegate(int a, int b) { return b - a; });
Show(numbers);
// *** in version 3.0: ***
Show(numbers.FindAll(n => n > 5)); // the type is deduced
numbers.Sort((a, b) => a - b);
Show(numbers);
// a lambda is stored in a delegate variable
Func<int, int> square = n => n * n;
Console.WriteLine(square(7));
// several statements need braces and an explicit return
Func<int, string> describe = n => {
if (n > 5) return "big";
return "small";
};
Console.WriteLine(describe(9));
// a lambda without a result
Action<string> log = text => Console.WriteLine("log: " + text);
log("done");
}
}