如何在 C# 中将第二个列表追加到现有列表?
使用 AddRange() 方法 将第二个列表追加到现有列表。
以下是第一个列表 −
List < string > list1 = new List < string > ();
list1.Add("One");
list1.Add("Two");以下是第二个列表 −
List < string > list2 = new List < string > ();
list2.Add("Three");
ist2.Add("Four");现在我们进行追加 −
list1.AddRange(list2);
我们来看一下完整的代码。
示例
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
List < string > list1 = new List < string > ();
list1.Add("One");
list1.Add("Two");
Console.WriteLine("First list...");
foreach(string value in list1) {
Console.WriteLine(value);
}
Console.WriteLine("Second list...");
List < string > list2 = new List < string > ();
list2.Add("Three");
list2.Add("Four");
foreach(string value in list2) {
Console.WriteLine(value);
}
Console.WriteLine("After Append...");
list1.AddRange(list2);
foreach(string value in list1) {
Console.WriteLine(value);
}
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP