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

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


2つのHybridDictionaryオブジェクトが等しいかどうかを確認するには、Equals()メソッドを使用します。HybridDictionaryは、System.Collections.Specialized名前空間に属するコレクションクラスで、要素数が少ないうちはListDictionaryとして動作し、要素が増えるとHashtableへ自動的に切り替わるという特徴を持っています。

ここで重要なのは、Equals()メソッドが既定では参照の等価性(同じインスタンスを参照しているかどうか)を比較するという点です。そのため、中身がまったく同じでも別々に生成されたインスタンス同士を比較するとFalseになり、同一インスタンスへの参照である場合にのみTrueが返されます。

例1:同一インスタンスを比較する場合

次の例では、dict3にdict2を代入した後、Equals()メソッドを使って両者が等しいかどうかを確認しています。

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      HybridDictionary dict1 = new HybridDictionary();
      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("HybridDictionary1 elements...");
      foreach(DictionaryEntry d in dict1){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      HybridDictionary dict2 = new HybridDictionary();
      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("\nHybridDictionary2 elements...");
      foreach(DictionaryEntry d in dict2){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      HybridDictionary dict3 = new HybridDictionary();
      dict3 = dict2;
      Console.WriteLine("\nIs HybridDictionary3 equal to HybridDictionary2? = "+(dict3.Equals(dict2)));
   }
}

出力

上記のコードを実行すると、次のような結果が出力されます。

HybridDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
HybridDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Is HybridDictionary3 equal to HybridDictionary2? = True

例2:異なるインスタンスを比較する場合

続いて、それぞれ独立して生成された2つのHybridDictionaryを比較する例を見てみましょう。

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      HybridDictionary dict1 = new HybridDictionary();
      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("HybridDictionary1 elements...");
      foreach(DictionaryEntry d in dict1){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      HybridDictionary dict2 = new HybridDictionary();
      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("\nHybridDictionary2 elements...");
      foreach(DictionaryEntry d in dict2){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("\nIs HybridDictionary1 equal to HybridDictionary2? = "+ (dict1.Equals(dict2)));
   }
}

出力

このコードを実行すると、次のような結果が出力されます。

HybridDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
HybridDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Is HybridDictionary1 equal to HybridDictionary2? = False

まとめ

HybridDictionaryのEquals()メソッドは、既定では参照の等価性を判定します。同一インスタンスへの参照であればTrue、別々に生成されたインスタンスであればFalseを返します。キーと値の中身まで厳密に比較したい場合は、foreachで各要素を順番に照合するなど、独自の比較ロジックを実装する必要がある点に注意しましょう。

  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