두 목록을 결합하려면 AddRange() 메서드를 사용하십시오.
첫 번째 목록 설정 -
var list1 = new List < string > ();
list1.Add("Keyboard");
list1.Add("Mouse"); 두 번째 목록 설정 -
var list2 = new List < string > ();
list2.Add("Hard Disk");
list2.Add("Pen Drive"); 두 목록을 연결하려면 -
lists1.AddRange(lists2);
다음은 완전한 코드입니다 -
예시
using System.Collections.Generic;
using System;
namespace Demo {
public static class Program {
public static void Main() {
var list1 = new List < string > ();
list1.Add("Keyboard");
list1.Add("Mouse");
Console.WriteLine("Our list1....");
foreach(var p in list1) {
Console.WriteLine(p);
}
var list2 = new List < string > ();
list2.Add("Hard Disk");
list2.Add("Pen Drive");
Console.WriteLine("Our list2....");
foreach(var p in list2) {
Console.WriteLine(p);
}
list1.AddRange(list2);
Console.WriteLine("Concatenated list....");
foreach(var p in list1) {
Console.WriteLine(p);
}
}
}
}