温馨提示×

如何遍历C#中的List集合

c#
小樊
356
2024-06-28 20:34:27
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在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循环遍历集合

0