在 .NET 6 中,可以使用 Zip 方法将两个列表一起循环。Zip 方法将两个列表的元素一一配对,然后可以对这些配对进行操作。

以下是一个示例代码:

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> list1 = new List<int> { 1, 2, 3 };
        List<string> list2 = new List<string> { "A", "B", "C" };

        var result = list1.Zip(list2, (num, str) => $"{num} - {str}");

        foreach (var item in result)
        {
            Console.WriteLine(item);
        }
    }
}

输出:

1 - A
2 - B
3 - C

在上面的示例中,我们使用 Zip 方法将 list1 和 list2 两个列表配对,然后使用 lambda 表达式将配对的元素合并为一个字符串。最后,通过 foreach 循环遍历结果并输出

net6 两个list一起循环

原文地址: https://www.cveoy.top/t/topic/iiWS 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录