Check if two StringCollection objects are equal in C#

In C#, checking if two StringCollection objects are equal is not as straightforward as it might seem. The default Equals() method only checks for reference equality, not content equality. This means two collections with identical elements will return false unless they reference the same object.

Understanding StringCollection Equality

The StringCollection class inherits the default Equals() method from Object, which performs reference comparison. To check for content equality, you need to implement custom comparison logic.

StringCollection Equality Types Reference Equality strCol1.Equals(strCol2) Returns true only if both variables point to the same object Usually FALSE Content Equality Custom comparison Compares each element and collection size for actual equality What we usually want

Using Default Equals() Method

Reference Equality Example

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringCollection strCol1 = new StringCollection();
      strCol1.Add("Accessories");
      strCol1.Add("Books");
      strCol1.Add("Electronics");
      
      Console.WriteLine("StringCollection1 elements...");
      foreach (string res in strCol1) {
         Console.WriteLine(res);
      }
      
      StringCollection strCol2 = new StringCollection();
      strCol2.Add("Accessories");
      strCol2.Add("Books");
      strCol2.Add("Electronics");
      
      Console.WriteLine("StringCollection2 elements...");
      foreach (string res in strCol2) {
         Console.WriteLine(res);
      }
      
      Console.WriteLine("Both collections equal (reference)? = " + strCol1.Equals(strCol2));
      
      StringCollection strCol3 = strCol2;
      Console.WriteLine("strCol3 equals strCol2 (same reference)? = " + strCol3.Equals(strCol2));
   }
}

The output of the above code is −

StringCollection1 elements...
Accessories
Books
Electronics
StringCollection2 elements...
Accessories
Books
Electronics
Both collections equal (reference)? = False
strCol3 equals strCol2 (same reference)? = True

Custom Content Equality Comparison

Using LINQ SequenceEqual

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

public class Demo {
   public static void Main() {
      StringCollection strCol1 = new StringCollection();
      strCol1.Add("Accessories");
      strCol1.Add("Books");
      strCol1.Add("Electronics");
      
      StringCollection strCol2 = new StringCollection();
      strCol2.Add("Accessories");
      strCol2.Add("Books");
      strCol2.Add("Electronics");
      
      StringCollection strCol3 = new StringCollection();
      strCol3.Add("Books");
      strCol3.Add("Accessories");
      strCol3.Add("Electronics");
      
      bool areEqual1 = strCol1.Cast<string>().SequenceEqual(strCol2.Cast<string>());
      bool areEqual2 = strCol1.Cast<string>().SequenceEqual(strCol3.Cast<string>());
      
      Console.WriteLine("strCol1 content equals strCol2? = " + areEqual1);
      Console.WriteLine("strCol1 content equals strCol3? = " + areEqual2);
   }
}

The output of the above code is −

strCol1 content equals strCol2? = True
strCol1 content equals strCol3? = False

Custom Comparison Method

Manual Implementation

using System;
using System.Collections.Specialized;

public class Demo {
   public static bool AreEqual(StringCollection col1, StringCollection col2) {
      if (col1.Count != col2.Count) return false;
      
      for (int i = 0; i < col1.Count; i++) {
         if (col1[i] != col2[i]) return false;
      }
      return true;
   }
   
   public static void Main() {
      StringCollection strCol1 = new StringCollection();
      strCol1.Add("Java");
      strCol1.Add("Python");
      strCol1.Add("C#");
      
      StringCollection strCol2 = new StringCollection();
      strCol2.Add("Java");
      strCol2.Add("Python");
      strCol2.Add("C#");
      
      Console.WriteLine("Using default Equals(): " + strCol1.Equals(strCol2));
      Console.WriteLine("Using custom comparison: " + AreEqual(strCol1, strCol2));
   }
}

The output of the above code is −

Using default Equals(): False
Using custom comparison: True

Comparison Methods

Method Comparison Type Order Sensitive
Default Equals() Reference equality N/A
LINQ SequenceEqual Content equality Yes
Custom method Content equality Configurable

Conclusion

The default Equals() method for StringCollection only checks reference equality, not content equality. To compare collections by their actual contents, use LINQ's SequenceEqual() method or implement a custom comparison function that iterates through elements.

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

209 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements