C# 中的 lambda 表达式是什么?
csharpprogrammingserver side programming更新于 2025/10/14 10:52:17
C# 中的 lambda 表达式描述一种模式。它在表达式上下文中具有标记 =>。它被读作"转到"运算符,并在声明 lambda 表达式时使用。
以下示例展示了如何在 C# 中使用 lambda 表达式 −
示例
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
List<int> list = new List<int>() { 21, 17, 40, 11, 9 };
int res = list.FindIndex(x => x % 2 == 0);
Console.WriteLine("Index: "+res);
}
}
输出
Index: 2
上面,我们看到了使用"goes to"运算符查找偶数减号的索引。
list.FindIndex(x => x % 2 == 0);
以上示例输出如下。
Index:2
偶数位于索引 2,即第 3 个元素。

