新版本的变更 / C# 3.0

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[] { 1469 });

        // *** 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<intint> square = n => n * n;
        Console.WriteLine(square(7));

        // several statements need braces and an explicit return
        Func<intstring> describe = n => {
            if (n > 5return "big";
            return "small";
        };
        Console.WriteLine(describe(9));

        // a lambda without a result
        Action<string> log = text => Console.WriteLine("log: " + text);
        log("done");
    }
}