C# 中使用 foreach 循环遍历集合
在 C# 中,可以使用 'foreach' 循环来遍历集合。以下是使用 'foreach' 循环遍历集合的示例代码:
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
Console.WriteLine(number);
}
在上述示例中,我们定义了一个 'List
1
2
3
4
5
请注意,'foreach' 循环只能用于实现了 'IEnumerable' 接口的集合类型。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/qumM 著作权归作者所有。请勿转载和采集!