如何在C#中使用一个foreach语句迭代两个列表或数组?
设置两个数组。
var val = new [] { 20, 40, 60};
var str = new [] { "ele1", "ele2", "ele3"};使用该zip()方法可以并行处理两个数组。
var res = val.Zip(str, (n, w) => new { Number = n, Word = w });上面分别获取了具有int和string元素的两个数组。
现在,使用foreach迭代两个数组-
示例
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
var val = new [] { 20, 40, 60};
var str = new [] { "ele1", "ele2", "ele3"};
var res = val.Zip(str, (n, w) => new { Number = n, Word = w });
foreach(var a in res) {
Console.WriteLine(a.Number + a.Word);
}
}
}输出结果
20ele1 40ele2 60ele3
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短