Check if two OrderedDictionary objects are equal in C#

The OrderedDictionary class in C# represents a collection of key-value pairs that are accessible by both key and index. When comparing two OrderedDictionary objects for equality, you need to understand that the default Equals() method performs reference equality, not content equality.

The Equals() method only returns true if both variables reference the same object in memory. For content-based comparison, you need a custom implementation.

Syntax

Following is the syntax for using the Equals() method −

bool isEqual = dict1.Equals(dict2);

Using Reference Equality (Default Behavior)

Different Objects with Same Content

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");
      
      Console.WriteLine("OrderedDictionary1 elements...");
      foreach(DictionaryEntry d in dict1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      OrderedDictionary dict2 = new OrderedDictionary();
      dict2.Add("A", "Books");
      dict2.Add("B", "Electronics");
      dict2.Add("C", "Smart Wearables");
      
      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)));
   }
}

The output of the above code is −

OrderedDictionary1 elements...
A Books
B Electronics
C Smart Wearables

OrderedDictionary2 elements...
A Books
B Electronics
C Smart Wearables

Is OrderedDictionary1 equal to OrderedDictionary2? = False

Same Object Reference

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      OrderedDictionary dict1 = new OrderedDictionary();
      dict1.Add("1", "One");
      dict1.Add("2", "Two");
      dict1.Add("3", "Three");
      
      Console.WriteLine("OrderedDictionary1 elements...");
      foreach(DictionaryEntry d in dict1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      OrderedDictionary dict2 = dict1; // Same reference
      
      Console.WriteLine("\nIs OrderedDictionary1 equal to dict2? = " + (dict1.Equals(dict2)));
   }
}

The output of the above code is −

OrderedDictionary1 elements...
1 One
2 Two
3 Three

Is OrderedDictionary1 equal to dict2? = True

Custom Content-Based Equality Method

To perform content-based comparison, you need a custom method that compares both keys and values −

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static bool AreEqual(OrderedDictionary dict1, OrderedDictionary dict2) {
      if (dict1.Count != dict2.Count) return false;
      
      for (int i = 0; i < dict1.Count; i++) {
         if (!dict1.Keys[i].Equals(dict2.Keys[i]) || 
             !dict1.Values[i].Equals(dict2.Values[i])) {
            return false;
         }
      }
      return true;
   }
   
   public static void Main() {
      OrderedDictionary dict1 = new OrderedDictionary();
      dict1.Add("A", "Apple");
      dict1.Add("B", "Banana");
      
      OrderedDictionary dict2 = new OrderedDictionary();
      dict2.Add("A", "Apple");
      dict2.Add("B", "Banana");
      
      OrderedDictionary dict3 = new OrderedDictionary();
      dict3.Add("A", "Apple");
      dict3.Add("B", "Orange");
      
      Console.WriteLine("Reference equality (dict1 vs dict2): " + dict1.Equals(dict2));
      Console.WriteLine("Content equality (dict1 vs dict2): " + AreEqual(dict1, dict2));
      Console.WriteLine("Content equality (dict1 vs dict3): " + AreEqual(dict1, dict3));
   }
}

The output of the above code is −

Reference equality (dict1 vs dict2): False
Content equality (dict1 vs dict2): True
Content equality (dict1 vs dict3): False

Key Differences

Comparison Type Method Returns True When
Reference Equality dict1.Equals(dict2) Both variables point to the same object
Content Equality Custom method Same keys, values, and order

Conclusion

The default Equals() method for OrderedDictionary objects performs reference equality, returning true only when both variables reference the same object. For content-based comparison, you need to implement a custom method that compares keys, values, and their order.

Updated on: 2026-03-17T07:04:36+05:30

160 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements