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