Check if two StringDictionary objects are equal or not in C#

The StringDictionary class in C# is a specialized collection that stores key-value pairs as strings. When comparing two StringDictionary objects for equality, it's important to understand that the default Equals()

Understanding StringDictionary Equality

The Equals() method for StringDictionary performs reference comparison, meaning it returns true only when both variables point to the same object in memory. Even if two dictionaries contain identical key-value pairs, they will not be considered equal unless they reference the same object.

StringDictionary Reference vs Content Equality dict1 A: John B: Andy C: Tim dict2 A: John B: Andy C: Tim ? Different objects Same content Equals() = false

Using Reference Assignment for Equality

Example

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "John");
      strDict1.Add("B", "Andy");
      strDict1.Add("C", "Tim");
      strDict1.Add("D", "Ryan");
      strDict1.Add("E", "Kevin");
      strDict1.Add("F", "Katie");
      strDict1.Add("G", "Brad");
      
      StringDictionary strDict2 = new StringDictionary();
      strDict2.Add("A", "John");
      strDict2.Add("B", "Andy");
      strDict2.Add("C", "Tim");
      strDict2.Add("D", "Ryan");
      strDict2.Add("E", "Kevin");
      strDict2.Add("F", "Katie");
      strDict2.Add("G", "Brad");
      
      StringDictionary strDict3 = new StringDictionary();
      strDict3 = strDict2;
      
      Console.WriteLine("Is Dictionary2 equal to Dictionary3? = " + strDict2.Equals(strDict3));
      Console.WriteLine("Is Dictionary1 equal to Dictionary3? = " + strDict1.Equals(strDict3));
   }
}

The output of the above code is −

Is Dictionary2 equal to Dictionary3? = True
Is Dictionary1 equal to Dictionary2? = False

Content Comparison Between StringDictionaries

The following example demonstrates that even dictionaries with identical content are not considered equal by the default Equals() method −

Example

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

public class Demo {
   public static void Main() {
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "John");
      strDict1.Add("B", "Andy");
      strDict1.Add("C", "Tim");
      strDict1.Add("D", "Ryan");
      strDict1.Add("E", "Kevin");
      strDict1.Add("F", "Katie");
      strDict1.Add("G", "Brad");
      
      Console.WriteLine("StringDictionary1 elements...");
      foreach(DictionaryEntry d in strDict1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      StringDictionary strDict2 = new StringDictionary();
      strDict2.Add("A", "John");
      strDict2.Add("B", "Andy");
      strDict2.Add("C", "Tim");
      strDict2.Add("D", "Ryan");
      strDict2.Add("E", "Kevin");
      strDict2.Add("F", "Katie");
      strDict2.Add("G", "Brad");
      
      Console.WriteLine("\nStringDictionary2 elements...");
      foreach(DictionaryEntry d in strDict2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      Console.WriteLine("\nIs Dictionary2 equal to Dictionary1? = " + strDict2.Equals(strDict1));
   }
}

The output of the above code is −

StringDictionary1 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad

StringDictionary2 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad

Is Dictionary2 equal to Dictionary1? = False

Custom Content Equality Method

To check if two StringDictionary objects have the same content, you need to implement custom comparison logic −

Example

using System;
using System.Collections.Specialized;

public class Demo {
   public static bool AreEqual(StringDictionary dict1, StringDictionary dict2) {
      if (dict1.Count != dict2.Count) return false;
      
      foreach (string key in dict1.Keys) {
         if (!dict2.ContainsKey(key) || dict1[key] != dict2[key])
            return false;
      }
      return true;
   }
   
   public static void Main() {
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "John");
      strDict1.Add("B", "Andy");
      strDict1.Add("C", "Tim");
      
      StringDictionary strDict2 = new StringDictionary();
      strDict2.Add("A", "John");
      strDict2.Add("B", "Andy");
      strDict2.Add("C", "Tim");
      
      Console.WriteLine("Reference equality: " + strDict1.Equals(strDict2));
      Console.WriteLine("Content equality: " + AreEqual(strDict1, strDict2));
   }
}

The output of the above code is −

Reference equality: False
Content equality: True

Conclusion

The Equals() method for StringDictionary performs reference comparison, not content comparison. Two dictionaries with identical key-value pairs will return false unless they reference the same object. For content-based equality checking, implement custom comparison logic that iterates through keys and values.

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

203 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements