Computer >> 컴퓨터 >  >> 프로그래밍 >> C#

C#에서 SortedList 요소를 배열 객체로 복사하는 방법

C#에서 SortedList의 모든 요소를 배열(Array) 객체로 복사하려면 CopyTo() 메서드를 사용합니다. 이 메서드는 SortedList의 키-값 쌍을 지정한 인덱스 위치부터 DictionaryEntry 형식의 1차원 배열에 순서대로 복사합니다.

CopyTo 메서드는 두 개의 매개변수를 받습니다. 첫 번째는 복사 대상이 되는 배열이고, 두 번째는 복사가 시작될 배열의 인덱스입니다. 아래 예제를 통해 실제 동작 방식을 확인해 보겠습니다.

예제 1: 특정 인덱스부터 복사하기

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args){
      SortedList list = new SortedList();
      list.Add("1", "AB");
      list.Add("2", "CD");
      list.Add("3", "EF");
      list.Add("4", "GH");
      list.Add("5", "IJ");
      list.Add("6", "JK");
      list.Add("7", "KL");
      list.Add("8", "LM");
      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in list){
         Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
      }
      Console.WriteLine("\nCopied to Array object...");
      DictionaryEntry[] dictArr = new DictionaryEntry[10];
      list.CopyTo(dictArr, 2);
      for (int i = 0; i < dictArr.Length; i++) {
         Console.WriteLine("Key = "+ dictArr[i].Key + ", Value = " + dictArr[i].Value);
      }
   }
}

출력 결과

위 코드를 실행하면 다음과 같은 결과가 출력됩니다.

SortedList elements...
Key = 1, Value = AB
Key = 2, Value = CD
Key = 3, Value = EF
Key = 4, Value = GH
Key = 5, Value = IJ
Key = 6, Value = JK
Key = 7, Value = KL
Key = 8, Value = LM

Copied to Array object...
Key = , Value =
Key = , Value =
Key = 1, Value = AB
Key = 2, Value = CD
Key = 3, Value = EF
Key = 4, Value = GH
Key = 5, Value = IJ
Key = 6, Value = JK
Key = 7, Value = KL
Key = 8, Value = LM

위 예제에서는 크기가 10인 배열을 생성한 뒤, 인덱스 2부터 복사를 시작했습니다. 그 결과 배열의 앞쪽 두 칸(인덱스 0과 1)은 비어 있는 상태로 남고, 인덱스 2부터 SortedList의 요소가 채워지는 것을 확인할 수 있습니다.

예제 2: 배열 처음부터 복사하기

이번에는 시작 인덱스를 0으로 지정하여 배열의 맨 앞부터 요소를 복사하는 예제를 살펴보겠습니다.

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args){
      SortedList list = new SortedList();
      list.Add("1", "Katie");
      list.Add("2", "Andy");
      list.Add("3", "Mark");
      list.Add("4", "Gary");
      list.Add("5", "Sam");
      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in list){
         Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
      }
      Console.WriteLine("\nCopied to Array object...");
      DictionaryEntry[] dictArr = new DictionaryEntry[5];
      list.CopyTo(dictArr, 0);
      for (int i = 0; i < dictArr.Length; i++) {
         Console.WriteLine("Key = "+ dictArr[i].Key + ", Value = " + dictArr[i].Value);
      }
   }
}

출력 결과

위 코드를 실행하면 다음과 같은 결과가 출력됩니다.

SortedList elements...
Key = 1, Value = Katie
Key = 2, Value = Andy
Key = 3, Value = Mark
Key = 4, Value = Gary
Key = 5, Value = Sam

Copied to Array object...
Key = 1, Value = Katie
Key = 2, Value = Andy
Key = 3, Value = Mark
Key = 4, Value = Gary
Key = 5, Value = Sam

시작 인덱스가 0이므로 SortedList의 모든 요소가 배열의 처음부터 순서대로 복사되었습니다. 참고로 복사 대상 배열의 크기는 시작 인덱스와 SortedList의 요소 수를 합친 값보다 크거나 같아야 하며, 그렇지 않으면 ArgumentException이 발생합니다.