在C#中,可以使用foreach循环来遍历List集合。以下是一个示例:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
Console.WriteLine(number);
}
}
}
在上面的示例中,我们首先创建了一个包含整数的List集合。然后,我们使用foreach循环遍历该列表,并打印每个元素的值。运行程序后,输出将是:
1
2
3
4
5
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:c#中怎么使用for循环遍历集合