=> 标记称作 lambda 运算符。 该标记在 lambda 表达式中用来将左侧的输入变量与右侧的 lambda 体分离。 Lambda 表达式是与匿名方法类似的内联表达式,但更加灵活;在以方法语法表示的 LINQ 查询中广泛使用了 Lambda 表达式。 有关更多信息,请参见Lambda 表达式(C# 编程指南)。
下面的示例演示两种查找并显示最短的字符串的长度在字符数组中的字符串。 该示例的第一部分将 lambda 表达式 (w => w.Length) words 于数组的每个元素都使用 Min``1 方法查找最小长度。 为了进行比较,该示例的第二部分演示一个较长的解决方案使用查询语法执行相同操作。
string[] words = { "cherry", "apple", "blueberry" }; // Use method syntax to apply a lambda expression to each element // of the words array. int shortestWordLength = words.Min(w => w.Length); Console.WriteLine(shortestWordLength); // Compare the following code that uses query syntax. // Get the lengths of each word in the words array. var query = from w in words select w.Length; // Apply the Min method to execute the query and get the shortest length. int shortestWordLength2 = query.Min(); Console.WriteLine(shortestWordLength2); // Output: // 5 // 5
下面的代码演示如何编写采用两个参数标准查询运算符 Enumerable.Where``1 的超加载的 lambda 表达式。 由于 lambda 表达式具有多个参数,必须括在括号中的参数。 第二个参数,index,表示当前元素的索引集合中的。 Where 表达式返回长度小于其在数组的索引位置小于的任何字符串。
static void Main(string[] args) { string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; Console.WriteLine("Example that uses a lambda expression:"); var shortDigits = digits.Where((digit, index) => digit.Length < index); foreach (var sD in shortDigits) { Console.WriteLine(sD); } // Compare the following code, which arrives at the same list of short // digits but takes more work to get there. Console.WriteLine("\nExample that uses a for loop:"); List<string> shortDigits2 = new List<string>(); for (var i = 0; i < digits.Length; i++) { if (digits[i].Length < i) shortDigits2.Add(digits[i]); } foreach (var d in shortDigits2) { Console.WriteLine(d); } // Output: // Example that uses a lambda expression: // five // six // seven // eight // nine // Example that uses a for loop: // five // six // seven // eight // nine }