直接上示例代码进行说明:
1. from-where-select的用法:
int[] arr = {3, 4, 5, 6, 7, 8, 9};
int[] res = (from a in arr
where a > 4 && a < 9
select a).ToArray();
List<int> lst = new List<int>(res);
foreach (var item in lst) => Console.WriteLine($"item");
2. 运行结果:
5
6
7
8
3. 以上from-where-select用法可以for用法解读:
for (int i = 0; i < res.Length; i++)
{
if (res[i] > 4 && res[i] < 9)
{
//TODO
}
}
本文介绍了如何在C#中使用from-where-select语法从整数数组中筛选元素,以及如何用for循环实现相同功能。通过给定的示例代码和运行结果,展示了这种方法的用法和for循环的对比。
1140

被折叠的 条评论
为什么被折叠?



