C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#で2つのListDictionaryオブジェクトが等しいかどうかを確認する方法

C#のListDictionaryコレクションにおいて、2つのオブジェクトが等しいかどうかを判定するには、Equals()メソッドを使用します。この記事では、具体的なコード例とその実行結果を通じて、Equals()メソッドの動作を詳しく解説します。

ListDictionaryのEquals()メソッドとは

ListDictionarySystem.Collections.Specialized名前空間に属するコレクションクラスで、キーと値のペアをリスト形式で格納します。Equals()メソッドは、指定されたオブジェクトが現在のインスタンスと同一であるかどうかを判定します。重要なポイントとして、ListDictionaryEquals()をオーバーライドしていないため、この比較は内容ではなく参照(同一インスタンスかどうか)に基づいて行われます。

例1:同じ参照を持つListDictionaryの比較

まず、ある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 elements...");
      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 elements...");
      foreach(DictionaryEntry d in dict2){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      ListDictionary dict3 = new ListDictionary();
      dict3 = dict2;
      Console.WriteLine("\nIs ListDictionary3 equal to ListDictionary2? = "+(dict3.Equals(dict2)));
   }
}

出力結果

上記のコードを実行すると、次の出力が得られます。

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

ListDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Is ListDictionary3 equal to ListDictionary2? = True

ここでdict3 = dict2;という代入により、dict3にはdict2と同じオブジェクトへの参照が格納されます。そのため、dict3.Equals(dict2)Trueを返します。

例2:別々のインスタンス同士の比較

次に、内容が異なる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 elements...");
      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 elements...");
      foreach(DictionaryEntry d in dict2){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("\nIs ListDictionary1 equal to ListDictionary2? = "+(dict1.Equals(dict2)));
   }
}

出力結果

このコードを実行すると、以下の出力が得られます。

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

ListDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Is ListDictionary1 equal to ListDictionary2? = False

まとめ:Equals()メソッドの挙動のポイント

これらの例から、以下のことがわかります。

  • ListDictionaryEquals()メソッドは、要素の内容(キーと値の組み合わせ)を比較するのではなく、オブジェクトの参照が同一かどうかを判定します。
  • ある変数に別のListDictionaryを代入した場合、両者は同じインスタンスを指すため、Equals()Trueを返します。
  • たとえ格納されている要素がまったく同じでも、それぞれ独立して生成されたインスタンス同士の比較ではFalseが返されます。

もし2つのListDictionaryの「中身」が等しいかどうかを確認したい場合は、各要素をループで取り出してキーと値を個別に比較するか、LINQなどを活用して独自に比較ロジックを実装する必要があります。

  1. Python Pandas – 2つのIndexオブジェクトが等しいかどうかを判定する方法

    Pandasで2つのIndexオブジェクトが等しいかどうかを確認するには、equals()メソッドを使用します。このメソッドは、両方のインデックスの要素と順序が完全に一致している場合にTrueを返し、そうでなければFalseを返します。equals()メソッドの基本的な使い方まず、必要なライブラリをインポートします。import pandas as pd次に、比較対象となるindex1とindex2を作成します。index1 = pd.Index([15, 25, 55, 10, 100, 70, 35, 40, 55]) index2 = pd.Index([15, 25, 55, 10,

  2. Python Pandas入門:DataFrameオブジェクトが等しいかどうかを確認する方法

    PythonのPandasで2つのDataFrame(データフレーム)オブジェクトが等しいかどうかを確認するには、equals()メソッドを使用します。このメソッドは、2つのDataFrameが同じ形状・要素・値を持っている場合にTrueを返し、そうでなければFalseを返します。DataFrame1の作成まず、2つの列(Car、Reg_Price)を持つDataFrame1を作成しましょう。dataFrame1 = pd.DataFrame( { Car: [BMW, Lexus, Audi, Mustang, Bentley, Jaguar], Reg_Pri