Check if two LinkedList objects are equal in C#

To check if two LinkedList objects are equal in C#, you need to understand that the Equals() method performs reference equality by default, not content equality. This means it only returns true if both references point to the same object instance.

Understanding Reference vs Content Equality

The LinkedList<T> class inherits the default Equals() method from Object, which compares object references rather than the actual contents of the lists.

LinkedList Equality Types Reference Equality list1.Equals(list2) Returns true only if both variables point to the same object list1 = list2 Content Equality SequenceEqual() Compares each element in both lists for value equality {1,2,3} == {1,2,3}

Using Reference Equality (Default Equals Method)

Example 1: Same Object Reference

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      LinkedList<string> list1 = new LinkedList<string>();
      list1.AddLast("One");
      list1.AddLast("Two");
      list1.AddLast("Three");

      LinkedList<string> list2 = new LinkedList<string>();
      list2.AddLast("India");
      list2.AddLast("US");
      list2.AddLast("UK");

      LinkedList<string> list3 = list2; // Same reference

      Console.WriteLine("Is list3 equal to list2? " + list3.Equals(list2));
      Console.WriteLine("Is list1 equal to list2? " + list1.Equals(list2));
   }
}

The output of the above code is −

Is list3 equal to list2? True
Is list1 equal to list2? False

Example 2: Different Object References

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      LinkedList<string> list1 = new LinkedList<string>();
      list1.AddLast("Apple");
      list1.AddLast("Banana");

      LinkedList<string> list2 = new LinkedList<string>();
      list2.AddLast("Apple");
      list2.AddLast("Banana");

      Console.WriteLine("Same content, different objects:");
      Console.WriteLine("list1.Equals(list2): " + list1.Equals(list2));
   }
}

The output of the above code is −

Same content, different objects:
list1.Equals(list2): False

Using Content Equality with SequenceEqual

To compare the actual contents of two LinkedList objects, use the SequenceEqual() method from LINQ −

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

public class Demo {
   public static void Main() {
      LinkedList<string> list1 = new LinkedList<string>();
      list1.AddLast("Apple");
      list1.AddLast("Banana");
      list1.AddLast("Cherry");

      LinkedList<string> list2 = new LinkedList<string>();
      list2.AddLast("Apple");
      list2.AddLast("Banana");
      list2.AddLast("Cherry");

      LinkedList<string> list3 = new LinkedList<string>();
      list3.AddLast("Apple");
      list3.AddLast("Cherry");
      list3.AddLast("Banana");

      Console.WriteLine("Content comparison using SequenceEqual:");
      Console.WriteLine("list1.SequenceEqual(list2): " + list1.SequenceEqual(list2));
      Console.WriteLine("list1.SequenceEqual(list3): " + list1.SequenceEqual(list3));

      Console.WriteLine("\nReference comparison using Equals:");
      Console.WriteLine("list1.Equals(list2): " + list1.Equals(list2));
   }
}

The output of the above code is −

Content comparison using SequenceEqual:
list1.SequenceEqual(list2): True
list1.SequenceEqual(list3): False

Reference comparison using Equals:
list1.Equals(list2): False

Comparison of Equality Methods

Method Comparison Type Result
Equals() Reference equality True only if same object reference
SequenceEqual() Content equality True if same elements in same order
ReferenceEquals() Reference equality True only if same object reference

Conclusion

The Equals() on LinkedList objects performs reference equality, returning true only when both variables point to the same object. For content comparison, use SequenceEqual() from LINQ, which compares elements in order and returns true if both lists contain the same elements in the same sequence.

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

177 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements