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

C#에서 두 ListDictionary 객체가 동일한지 확인하는 방법

C#에서 두 개의 ListDictionary 객체가 동일한지 확인하려면 Equals() 메서드를 사용합니다. 이 메서드는 두 객체가 같은 인스턴스를 참조하는지 여부에 따라 true 또는 false를 반환합니다.

예제 1: 동일한 객체를 참조하는 경우

다음 예제에서는 세 개의 ListDictionary를 생성하고, 하나의 변수가 다른 변수와 같은 객체를 참조하도록 할당한 후 동일성을 비교해 보겠습니다.

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      ListDictionary dict1 = new ListDictionary();
      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("ListDictionary1 요소...");
      foreach(DictionaryEntry d in dict1){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      ListDictionary dict2 = new ListDictionary();
      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("\nListDictionary2 요소...");
      foreach(DictionaryEntry d in dict2){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      ListDictionary dict3 = new ListDictionary();
      dict3 = dict2;
      Console.WriteLine("\nListDictionary3은 ListDictionary2와 동일한가? = "+(dict3.Equals(dict2)));
   }
}

출력 결과

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

ListDictionary1 요소...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear

ListDictionary2 요소...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
ListDictionary3은 ListDictionary2와 동일한가? = True

이 예제에서 dict3 = dict2; 구문으로 인해 dict3는 dict2와 동일한 객체를 참조하게 됩니다. 따라서 Equals() 메서드는 true를 반환합니다.

예제 2: 서로 다른 객체를 비교하는 경우

이번에는 내용이 다른 두 개의 독립적인 ListDictionary 객체를 비교해 보겠습니다.

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      ListDictionary dict1 = new ListDictionary();
      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("ListDictionary1 요소...");
      foreach(DictionaryEntry d in dict1){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      ListDictionary dict2 = new ListDictionary();
      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("\nListDictionary2 요소...");
      foreach(DictionaryEntry d in dict2){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("\nListDictionary1은 ListDictionary2와 동일한가? = "+(dict1.Equals(dict2)));
   }
}

출력 결과

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

ListDictionary1 요소...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear

ListDictionary2 요소...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
ListDictionary1은 ListDictionary2와 동일한가? = False

핵심 정리

  • ListDictionary의 Equals() 메서드는 기본적으로 참조(reference) 동일성을 비교합니다.
  • 두 변수가 같은 객체 인스턴스를 가리키면 true, 서로 다른 별개의 객체라면 내용이 같더라도 false를 반환합니다.
  • 딕셔너리의 키-값 쌍 내용 자체를 비교하려면 각 항목을 직접 순회하며 비교하는 별도의 로직을 구현해야 합니다.