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

C#에서 지정한 인덱스부터 ListDictionary를 배열(Array)로 복사하는 방법

C#의 ListDictionary 컬렉션을 지정한 인덱스 위치부터 배열(Array) 인스턴스로 복사하려면 CopyTo() 메서드를 사용합니다. 이 메서드는 두 개의 매개변수를 받는데, 첫 번째는 복사 대상이 될 1차원 배열이고, 두 번째는 복사가 시작될 배열의 인덱스입니다.

아래 예제를 통해 실제 사용 방법을 살펴보겠습니다.

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

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      ListDictionary dict = new ListDictionary();
      dict.Add(1, "Harry");
      dict.Add(2, "Mark");
      dict.Add(3, "John");
      dict.Add(4, "Jacob");
      dict.Add(5, "Tim");
      dict.Add(6, "Sam");
      dict.Add(7, "Tom");
      dict.Add(8, "Kevin");

      Console.WriteLine("ListDictionary elements...");
      foreach(DictionaryEntry d in dict) {
         Console.WriteLine(d.Key + " " + d.Value);
      }

      DictionaryEntry[] dictArr = new DictionaryEntry[dict.Count];
      Console.WriteLine("\nCopying to Array instance...");
      dict.CopyTo(dictArr, 0);

      for (int i = 0; i < dictArr.Length; i++) {
         Console.WriteLine("Key = " + dictArr[i].Key + ", Value = " + dictArr[i].Value);
      }
   }
}

실행 결과

ListDictionary elements...
1 Harry
2 Mark
3 John
4 Jacob
5 Tim
6 Sam
7 Tom
8 Kevin

Copying to Array instance...
Key = 1, Value = Harry
Key = 2, Value = Mark
Key = 3, Value = John
Key = 4, Value = Jacob
Key = 5, Value = Tim
Key = 6, Value = Sam
Key = 7, Value = Tom
Key = 8, Value = Kevin

위 예제에서는 배열 크기를 딕셔너리 요소 개수(dict.Count)와 동일하게 생성하고, 인덱스 0부터 복사했기 때문에 모든 요소가 순서대로 배열에 저장되었습니다.

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

이번에는 시작 인덱스를 5로 지정하여, 배열의 앞부분은 비워두고 중간부터 데이터가 채워지도록 해보겠습니다.

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      ListDictionary dict = new ListDictionary();
      dict.Add(1, "Harry");
      dict.Add(2, "Mark");
      dict.Add(3, "John");
      dict.Add(4, "Jacob");
      dict.Add(5, "Tim");

      Console.WriteLine("ListDictionary elements...");
      foreach(DictionaryEntry d in dict) {
         Console.WriteLine(d.Key + " " + d.Value);
      }

      DictionaryEntry[] dictArr = new DictionaryEntry[10];
      Console.WriteLine("\nCopying to Array instance...");
      dict.CopyTo(dictArr, 5);

      for (int i = 0; i < dictArr.Length; i++) {
         Console.WriteLine("Key = " + dictArr[i].Key + ", Value = " + dictArr[i].Value);
      }
   }
}

실행 결과

ListDictionary elements...
1 Harry
2 Mark
3 John
4 Jacob
5 Tim

Copying to Array instance...
Key = , Value =
Key = , Value =
Key = , Value =
Key = , Value =
Key = , Value =
Key = 1, Value = Harry
Key = 2, Value = Mark
Key = 3, Value = John
Key = 4, Value = Jacob
Key = 5, Value = Tim

핵심 정리

  • CopyTo(array, index): ListDictionary의 모든 DictionaryEntry 요소를 지정한 배열의 해당 인덱스부터 순서대로 복사합니다.
  • 배열의 크기는 (시작 인덱스 + 딕셔너리 요소 수) 이상이어야 하며, 그렇지 않으면 ArgumentException 또는 ArgumentOutOfRangeException이 발생할 수 있습니다.
  • 복사되지 않은 배열의 나머지 공간은 기본값(DictionaryEntry의 경우 빈 값)으로 유지됩니다. 위 예제 2의 실행 결과에서 인덱스 0~4가 비어 있는 것을 확인할 수 있습니다.