AddRange() 메서드를 사용하여 기존 목록에 두 번째 목록을 추가합니다.
다음은 목록 1입니다 -
List < string > list1 = new List < string > ();
list1.Add("One");
list1.Add("Two"); 다음은 목록 2입니다 -
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);
}
}
}