C#에서 두 개의 OrderedDictionary 개체가 동일한지 확인하려면 Equals() 메서드를 사용하면 됩니다. 이 메서드는 두 개체가 같은 참조(인스턴스)를 가리키고 있는지를 판단하여 그 결과를 불리언 값으로 반환합니다.
예제 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)));
}
}
출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
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
실행 결과에서 볼 수 있듯이 두 사전은 내용이 완전히 다르기 때문에 Equals() 메서드는 False를 반환했습니다.
예제 2 – 동일한 참조를 가진 사전 비교하기
이번에는 한 사전을 다른 변수에 할당한 후 두 개체를 비교해 보겠습니다.
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);
}
OrderedDictionary dict3 = new OrderedDictionary();
dict3 = dict2;
Console.WriteLine("\nIs OrderedDictionary3 equal to OrderedDictionary2? = " + (dict3.Equals(dict2)));
}
}
출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
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 OrderedDictionary3 equal to OrderedDictionary2? = True
dict3 = dict2; 구문을 통해 dict2의 참조가 dict3에 할당되었으므로 두 변수는 같은 개체를 가리킵니다. 따라서 이번에는 Equals() 메서드가 True를 반환했습니다.
정리
OrderedDictionary 클래스는 Object.Equals()를 재정의하지 않기 때문에 기본적으로 참조 동등성(reference equality)을 기준으로 비교합니다. 즉, 두 사전의 내용이 같더라도 서로 다른 인스턴스라면 False가 반환됩니다. 만약 키와 값의 실제 내용까지 비교하고 싶다면 두 컬렉션의 요소를 직접 순회하며 하나씩 대조하는 별도의 로직을 작성해야 합니다.