如何将第二个列表追加到 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