Lambda表達式 簡化了匿名委托的使用,讓你讓代碼更加簡潔,優雅。據說它是微軟自c#1.0後新增的最重要的功能之一。
首先來看一下其發展
根據上面的發展歷程,可以感到Lambda表達式愈加簡化。
詳細介紹:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Lambda { class Program { static void Main(string[] args) { int sum = 0; Func<int, int> f1 = x => 2 * x + 1; Func<int, int, bool> f2 = (x, y) => x > y; Func<string, int, string> f3 = (x, y) => x.Substring(y); Func<int, int> f4 = (x) => sum += x; Action a1 = () => { System.Console.WriteLine("HelloWorld"); }; Action <int> a2 = (x) => { System.Console.WriteLine(x); }; Action <bool> a3 = (x) => { System.Console.WriteLine(x); }; Action<string> a4 = (x) => { System.Console.WriteLine(x); }; for (int i = 1; i <= 10; i++ ) { f4(i); } a2(sum); a1(); a2(f1(1)); a3(f2(3, 5)); a4(f3("Zhengpengfei", 5)); System.Console.Read(); } } }