Lambda表達式可分為表達式lambda和語句lambda
表達式lambda:表達式位於 => 運算符右側的lambda表達式稱為表達式lambda
(input parameters) => expression //表達式lambda
例如
(x, y) => x == y
語句lambda:=> 運算符右側是一個語句塊,語句包含在大括號中
(input parameters) => {statement;} //語句lambda
例如:
(x, y) => {return x == y;}
這兩者除了寫法不一樣,還有什麼區別嗎,用以下代碼作為測試
using System; using System.Collections.Generic; namespace LinqTest { class Program { static void Main(string[] args) { List<int> list = new List<int> { 1, 3, 2, 4 }; var resultUsingExpressionLambda = list.FindAll(p => p < 3); Console.WriteLine("使用表達式lambda:"); foreach (var item in resultUsingExpressionLambda) { Console.WriteLine(item); } var resultUsingStatementLambda = list.FindAll(p => { return p < 3; }); Console.WriteLine("使用語句lambda:"); foreach (var item in resultUsingStatementLambda) { Console.WriteLine(item); } } } }
代碼比較簡單,就是分別使用表達式lambda和語句lambda找出小於3的數,然後輸出來,結果如下
using System; using System.Collections.Generic; namespace LinqTest { class Program { static void Main(string[] args) { List<int> list = new List<int> { 1, 3, 2, 4 }; var resultUsingExpressionLambda = list.FindAll(p => p < 3); Console.WriteLine("使用表達式lambda:"); foreach (var item in resultUsingExpressionLambda) { Console.WriteLine(item); } var resultUsingStatementLambda = list.FindAll(p => { Console.WriteLine(p);//這是新加的 return p < 3; }); Console.WriteLine("使用語句lambda:"); foreach (var item in resultUsingStatementLambda) { Console.WriteLine(item); } } } }
再查看反編譯後的代碼
Expression<Func<int, int, int>> expression = (a, b) => a + b;//正確 Expression<Func<int, int, int>> expression1 = (a, b) => { return a + b; };//錯誤,無法將具有語句體的 lambda 表達式轉換為表達式樹