C# 中的 Lambda 表达式

csharpprogrammingserver side programming更新于 2025/8/13 0:52:17

C# 中的 Lambda 表达式描述了一种模式。

Lambda 表达式在表达式上下文中有一个标记 =>。它被读作"转到"运算符,并在声明 Lambda 表达式时使用。

这里,我们从列表中查找大于 50 的元素的第一个出现位置。

list.FindIndex(x => x > 50);

上面使用了标记 =>。如下所示 −

示例

using System;
using System.Collections.Generic;

class Demo {
   static void Main() {
      List<int> list = new List<int> { 44, 6, 34, 23, 78 };

      int res = list.FindIndex(x => x > 50);
      Console.WriteLine("Index: "+res);
   }
}

输出

Index: 4

相关文章