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

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

C#で2つのOrderedDictionaryオブジェクトが等しいかどうかを確認するには、Equals()メソッドを使用します。なお、OrderedDictionaryクラスはObject.Equals()をオーバーライドしていないため、このメソッドは要素の中身ではなく「同一インスタンス(参照)かどうか」を判定します。つまり、内容が同じでも別々に生成されたオブジェクトはFalseとなり、同じ参照を代入した変数同士は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)));
   }
}

出力

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

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

dict1とdict2はそれぞれ独立して生成された別インスタンスのため、Equals()Falseを返します。

例2:同じ参照を持つOrderedDictionaryを比較する

続いて、ある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);
      }
      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同士の等価性をEquals()で判定する場合は、「参照の一致」が基準になる点に注意してください。もし要素(キーと値)の内容自体を比較したい場合は、各エントリを順番に取り出して自分で比較処理を実装する必要があります。

  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