gpt4 book ai didi

c# - 在 c# 中对两个列表使用 foreach 循环以获取列表值

转载 作者:行者123 更新时间:2023-11-30 13:13:24 25 4
gpt4 key购买 nike

List<String> listA = new List<string> { "A1", "A2" };
List<String> listB = new List<string> { "B1", "B2" };

for(int i = 0; i < listA.Count; i++)
{
text += listA[i] + " and " + listB[i];
}

如何使用 foreach 循环执行此操作?

最佳答案

您可以使用 Linq 和 Zip方法:

List<String> listA = new List<string> { "A1", "A2" };
List<String> listB = new List<string> { "B1", "B2" };

foreach (var pair in listA.Zip(listB, (a,b) => new {A = a, B = b}))
{
text += pair.A + " and " + pair.B;
}

关于c# - 在 c# 中对两个列表使用 foreach 循环以获取列表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11410593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com