Check if two ArrayList objects are equal in C#

Checking if two ArrayList objects are equal in C# involves understanding how the Equals() method works. The ArrayList.Equals() method checks for reference equality, not content equality. This means it only returns true if both variables point to the same object in memory.

Understanding ArrayList.Equals() Behavior

The Equals()ArrayList performs reference comparison by default. Two separate ArrayList objects with identical contents will return false when compared using Equals().

ArrayList Reference vs Content Equality Reference Equality list1.Equals(list2) Same memory location? Content Equality Manual comparison Same elements? list3 = list1; // Reference assignment list3.Equals(list1) ? True

Using Reference Assignment

When you assign one ArrayList to another using the assignment operator, both variables reference the same object −

using System;
using System.Collections;

public class Demo {
   public static void Main(string[] args) {
      ArrayList list1 = new ArrayList();
      list1.Add("A");
      list1.Add("B");
      list1.Add("C");
      
      ArrayList list2 = new ArrayList();
      list2.Add("A");
      list2.Add("B");
      list2.Add("C");
      
      ArrayList list3 = list2; // Reference assignment
      
      Console.WriteLine("list1.Equals(list2): " + list1.Equals(list2));
      Console.WriteLine("list3.Equals(list2): " + list3.Equals(list2));
      Console.WriteLine("Are list2 and list3 the same reference? " + ReferenceEquals(list2, list3));
   }
}

The output of the above code is −

list1.Equals(list2): False
list3.Equals(list2): True
Are list2 and list3 the same reference? True

Checking Content Equality

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

using System;
using System.Collections;

public class Demo {
   public static bool AreArrayListsEqual(ArrayList list1, ArrayList list2) {
      if (list1.Count != list2.Count) return false;
      
      for (int i = 0; i < list1.Count; i++) {
         if (!list1[i].Equals(list2[i])) return false;
      }
      return true;
   }
   
   public static void Main(string[] args) {
      ArrayList list1 = new ArrayList();
      list1.Add("A");
      list1.Add("B");
      list1.Add("C");
      
      ArrayList list2 = new ArrayList();
      list2.Add("A");
      list2.Add("B");
      list2.Add("C");
      
      ArrayList list3 = new ArrayList();
      list3.Add("A");
      list3.Add("B");
      list3.Add("X");
      
      Console.WriteLine("Reference equality (list1 vs list2): " + list1.Equals(list2));
      Console.WriteLine("Content equality (list1 vs list2): " + AreArrayListsEqual(list1, list2));
      Console.WriteLine("Content equality (list1 vs list3): " + AreArrayListsEqual(list1, list3));
   }
}

The output of the above code is −

Reference equality (list1 vs list2): False
Content equality (list1 vs list2): True
Content equality (list1 vs list3): False

Using LINQ for Content Comparison

You can also use LINQ's SequenceEqual method for content comparison −

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

public class Demo {
   public static void Main(string[] args) {
      ArrayList list1 = new ArrayList() {"A", "B", "C"};
      ArrayList list2 = new ArrayList() {"A", "B", "C"};
      ArrayList list3 = new ArrayList() {"A", "B", "D"};
      
      bool isEqual1 = list1.Cast<object>().SequenceEqual(list2.Cast<object>());
      bool isEqual2 = list1.Cast<object>().SequenceEqual(list3.Cast<object>());
      
      Console.WriteLine("list1 and list2 have same content: " + isEqual1);
      Console.WriteLine("list1 and list3 have same content: " + isEqual2);
      Console.WriteLine("Reference equality (list1 vs list2): " + list1.Equals(list2));
   }
}

The output of the above code is −

list1 and list2 have same content: True
list1 and list3 have same content: False
Reference equality (list1 vs list2): False

Comparison Methods

Method Purpose Returns True When
Equals() Reference equality Both variables point to same object
Custom loop comparison Content equality All elements match in same positions
SequenceEqual() Content equality with LINQ All elements match in same order

Conclusion

The ArrayList.Equals() method checks reference equality, not content equality. To compare the actual contents of two ArrayList objects, you need to implement custom comparison logic or use LINQ's SequenceEqual method. Remember that reference assignment creates two variables pointing to the same object.

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

413 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements