Check if two ListDictionary objects are equal in C#

The ListDictionary class in C# is part of the System.Collections.Specialized namespace and provides a simple list-based dictionary implementation. To check if two ListDictionary objects are equal, you can use the Equals() method, which performs reference equality comparison by default.

Syntax

Following is the syntax for comparing two ListDictionary objects −

bool isEqual = listDict1.Equals(listDict2);

Understanding ListDictionary Equality

The Equals() method in ListDictionary performs reference equality, not content equality. This means it returns true only when both variables refer to the same object in memory, not when they contain the same key-value pairs.

ListDictionary Reference vs Content Equality Reference Equal dict1 = dict2 Same memory location Content Equal Same keys & values Different objects Equals() = True Equals() = False For content equality, use custom comparison methods

Using Reference Equality

Example − Same Reference

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

The output of the above code is −

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

ListDictionary2 elements...
1 One
2 Two
3 Three

Is ListDictionary3 equal to ListDictionary2? = True

Example − Different References

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");
        
        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");
        
        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)));
    }
}

The output of the above code is −

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

ListDictionary2 elements...
1 One
2 Two
3 Three

Is ListDictionary1 equal to ListDictionary2? = False

Custom Content Equality Comparison

For comparing the actual content of two ListDictionary objects, you need to implement custom comparison logic −

Example

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

public class Demo {
    public static bool AreEqual(ListDictionary dict1, ListDictionary dict2) {
        if (dict1.Count != dict2.Count) return false;
        
        foreach (DictionaryEntry entry in dict1) {
            if (!dict2.Contains(entry.Key) || !dict2[entry.Key].Equals(entry.Value)) {
                return false;
            }
        }
        return true;
    }
    
    public static void Main() {
        ListDictionary dict1 = new ListDictionary();
        dict1.Add("A", "Apple");
        dict1.Add("B", "Banana");
        
        ListDictionary dict2 = new ListDictionary();
        dict2.Add("A", "Apple");
        dict2.Add("B", "Banana");
        
        Console.WriteLine("Reference equality: " + dict1.Equals(dict2));
        Console.WriteLine("Content equality: " + AreEqual(dict1, dict2));
    }
}

The output of the above code is −

Reference equality: False
Content equality: True

Conclusion

The Equals() method in ListDictionary checks for reference equality, not content equality. For content-based comparison, you need to implement custom logic that compares keys and values. Use reference equality when you need to check if two variables point to the same ListDictionary object.

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

209 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements