使用 C# 程序合并两个或更多列表
设置三个列表 −
// three lists
var list1 = new List<int>{3, 4};
var list2 = new List<int>{1, 2, 3};
var list3 = new List<int>{2, 5, 6};现在,使用 Concat 方法合并上述列表 −
var res1 = list1.Concat(list2); var res2 = res1.Concat(list3);
下面是完整代码 −
示例
using System.Collections.Generic;
using System.Linq;
using System;
public class Demo {
public static void Main() {
// three lists
var list1 = new List<int>{3, 4};
var list2 = new List<int>{1, 2, 3};
var list3 = new List<int>{2, 5, 6};
// concat
var res1 = list1.Concat(list2);
var res2 = res1.Concat(list3);
foreach(int i in res2) {
Console.WriteLine(i);
}
}
}输出
3 4 1 2 3 2 5 6
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP