Check if two SortedList objects are equal in C#

To check if two SortedList objects are equal in C#, you can use the Equals() method. However, it's important to understand that this method checks for reference equality, not content equality. Two SortedList objects are considered equal only if they reference the same object in memory.

Syntax

Following is the syntax for checking equality between two SortedList objects −

bool isEqual = sortedList1.Equals(sortedList2);

Using Equals() for Reference Equality

The Equals() method returns true only when both variables reference the same SortedList object −

using System;
using System.Collections;

public class Demo {
   public static void Main(String[] args) {
      SortedList list1 = new SortedList();
      list1.Add("One", 1);
      list1.Add("Two", 2);
      list1.Add("Three", 3);
      list1.Add("Four", 4);
      list1.Add("Five", 5);
      
      Console.WriteLine("SortedList1 elements...");
      foreach(DictionaryEntry d in list1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      SortedList list2 = new SortedList();
      list2.Add("A", "Accessories");
      list2.Add("B", "Books");
      list2.Add("C", "Smart Wearable Tech");
      list2.Add("D", "Home Appliances");
      
      Console.WriteLine("\nSortedList2 elements...");
      foreach(DictionaryEntry d in list2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      SortedList list3 = list2; // Reference assignment
      Console.WriteLine("\nIs SortedList2 equal to SortedList3? = " + list3.Equals(list2));
   }
}

The output of the above code is −

SortedList1 elements...
Five 5
Four 4
One 1
Three 3
Two 2

SortedList2 elements...
A Accessories
B Books
C Smart Wearable Tech
D Home Appliances

Is SortedList2 equal to SortedList3? = True

Checking Different SortedList Objects

When comparing two different SortedList objects (even with identical content), Equals() returns false

using System;
using System.Collections;

public class Demo {
   public static void Main(String[] args) {
      SortedList list1 = new SortedList();
      list1.Add("One", 1);
      list1.Add("Two", 2);
      list1.Add("Three", 3);
      list1.Add("Four", 4);
      list1.Add("Five", 5);
      
      Console.WriteLine("SortedList1 elements...");
      foreach(DictionaryEntry d in list1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      SortedList list2 = new SortedList();
      list2.Add("A", "Accessories");
      list2.Add("B", "Books");
      list2.Add("C", "Smart Wearable Tech");
      list2.Add("D", "Home Appliances");
      
      Console.WriteLine("\nSortedList2 elements...");
      foreach(DictionaryEntry d in list2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      Console.WriteLine("\nIs SortedList2 equal to SortedList1? = " + list2.Equals(list1));
   }
}

The output of the above code is −

SortedList1 elements...
Five 5
Four 4
One 1
Three 3
Two 2

SortedList2 elements...
A Accessories
B Books
C Smart Wearable Tech
D Home Appliances

Is SortedList2 equal to SortedList1? = False

Checking Content Equality

To compare the actual contents of two SortedList objects, you need to implement a custom comparison method −

using System;
using System.Collections;

public class Demo {
   public static bool AreEqual(SortedList list1, SortedList list2) {
      if (list1.Count != list2.Count) return false;
      
      foreach (DictionaryEntry entry in list1) {
         if (!list2.ContainsKey(entry.Key) || !list2[entry.Key].Equals(entry.Value)) {
            return false;
         }
      }
      return true;
   }
   
   public static void Main(String[] args) {
      SortedList list1 = new SortedList();
      list1.Add("A", 1);
      list1.Add("B", 2);
      
      SortedList list2 = new SortedList();
      list2.Add("A", 1);
      list2.Add("B", 2);
      
      Console.WriteLine("Reference equality: " + list1.Equals(list2));
      Console.WriteLine("Content equality: " + AreEqual(list1, list2));
   }
}

The output of the above code is −

Reference equality: False
Content equality: True

Key Points

  • SortedList.Equals() performs reference equality comparison, not content comparison.

  • Two different SortedList objects will always return false with Equals(), even if they contain identical key-value pairs.

  • For content comparison, implement a custom method that compares counts, keys, and values.

Conclusion

The Equals() method for SortedList objects checks reference equality, not content equality. To compare the actual contents of two SortedList objects, you need to implement a custom comparison method that checks both keys and values.

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

171 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements