=> 运算符(C# 参考)

来源:互联网 发布:离线看书软件 编辑:程序博客网 时间:2024/06/04 23:20

=> 标记称作 lambda 运算符。 该标记在 lambda 表达式中用来将左侧的输入变量与右侧的 lambda 体分离。 Lambda 表达式是与匿名方法类似的内联表达式,但更加灵活;在以方法语法表示的 LINQ 查询中广泛使用了 Lambda 表达式。 有关更多信息,请参见Lambda 表达式(C# 编程指南)

下面的示例演示两种查找并显示最短的字符串的长度在字符数组中的字符串。 该示例的第一部分将 lambda 表达式 (w => w.Lengthwords 于数组的每个元素都使用 Min``1 方法查找最小长度。 为了进行比较,该示例的第二部分演示一个较长的解决方案使用查询语法执行相同操作。

C#
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

备注

=> 运算符具有与赋值运算符 (=) 相同的优先级,并且是右结合运算符。

可以显式指定输入变量的类型或让编译器推断类型;仍,该变量是强类型在编译时。 当指定类型时,必须将该类型括号中的名称和变量名,如下例所示。

int shortestWordLength = words.Min((string w) => w.Length);

示例

下面的代码演示如何编写采用两个参数标准查询运算符 Enumerable.Where``1 的超加载的 lambda 表达式。 由于 lambda 表达式具有多个参数,必须括在括号中的参数。 第二个参数,index,表示当前元素的索引集合中的。 Where 表达式返回长度小于其在数组的索引位置小于的任何字符串。

C#
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}
0 0
原创粉丝点击