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

C# OrderedDictionary 읽기 전용 복사본 만들기 – AsReadOnly() 메서드 활용법

OrderedDictionary의 읽기 전용(read-only) 복사본을 만들려면 AsReadOnly() 메서드를 사용하면 됩니다. 이 메서드는 원본 컬렉션을 감싸는 읽기 전용 래퍼(wrapper)를 반환하며, 반환된 개체의 IsReadOnly 속성 값은 True로 설정됩니다.


예제 1

두 개의 OrderedDictionary를 선언하고 비교한 후, 그중 하나에 대해 읽기 전용 복사본을 생성하는 예제입니다.

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

public class Demo {
   public static void Main() {
      OrderedDictionary dict1 = new OrderedDictionary();
      dict1.Add("A", "Books");
      dict1.Add("B", "Electronics");
      dict1.Add("C", "Smart Wearables");
      dict1.Add("D", "Pet Supplies");
      dict1.Add("E", "Clothing");
      dict1.Add("F", "Footwear");

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

      OrderedDictionary dict2 = new OrderedDictionary();
      dict2.Add("1", "One");
      dict2.Add("2", "Two");
      dict2.Add("3", "Three");
      dict2.Add("4", "Four");
      dict2.Add("5", "Five");
      dict2.Add("6", "Six");

      Console.WriteLine("\nOrderedDictionary2 elements...");
      foreach(DictionaryEntry d in dict2){
         Console.WriteLine(d.Key + " " + d.Value);
      }

      Console.WriteLine("\nIs OrderedDictionary1 equal to OrderedDictionary2? = " + (dict1.Equals(dict2)));

      OrderedDictionary orderedDict = dict2.AsReadOnly();
      Console.WriteLine("Is OrderedDictionary read-only? = " + orderedDict.IsReadOnly);
   }
}

실행 결과

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

OrderedDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear

OrderedDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six

Is OrderedDictionary1 equal to OrderedDictionary2? = False
Is OrderedDictionary read-only? = True

예제 2

이번에는 단일 OrderedDictionary만 사용하여 더 간단하게 확인해 보겠습니다.

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

public class Demo {
   public static void Main() {
      OrderedDictionary dict = new OrderedDictionary();
      dict.Add("A", "Books");
      dict.Add("B", "Electronics");
      dict.Add("C", "Smart Wearables");
      dict.Add("D", "Pet Supplies");
      dict.Add("E", "Clothing");
      dict.Add("F", "Footwear");

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

      OrderedDictionary orderedDict = dict.AsReadOnly();
      Console.WriteLine("Is OrderedDictionary read-only? = " + orderedDict.IsReadOnly);
   }
}

실행 결과

실행 결과는 다음과 같습니다.

OrderedDictionary elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear

Is OrderedDictionary read-only? = True

정리

AsReadOnly() 메서드는 원본 컬렉션 자체를 변경하지 않으면서 읽기 전용 뷰를 제공합니다. 따라서 컬렉션을 외부 코드나 다른 모듈에 노출해야 하는 상황에서 실수로 요소가 추가·삭제·수정되는 것을 방지하고 싶을 때 매우 유용합니다. 현재 개체가 읽기 전용 상태인지 여부는 IsReadOnly 속성을 통해 언제든지 확인할 수 있습니다.