-2

I am using two lists. Both of them have exactly the same properties and namings. Is it possible to check the both lists are equal or not?

public class Person { public string Name { get; set; } public int Age { get; set; } public Person(string name, int age) { this.Name = name; this.Age = age; } } var list1 = new List<Person>(); list1.Add(new Person("Dim", 12)); list1.Add(new Person("Dio", 13)); var list2 = new List<Person>(); list2.Add(new Person("Dim", 12)); list2.Add(new Person("Dio", 13)); bool listsEqual = list1 == list2; Debug.WriteLine("Lists are equal: " + listsEqual); 
11
  • Your Person class does not override the Equals method and == only checks reference-equality, you want to call object.Equals or Equals on your list Commented Aug 10, 2023 at 7:05
  • 1
    Check this: stackoverflow.com/questions/12795882/… Commented Aug 10, 2023 at 7:07
  • Is it good way to serialize them? var equal = JsonSerializer.Serialize(list1) == JsonSerializer.Serialize(list2); Commented Aug 10, 2023 at 7:08
  • @DmO no, it's orders of magnitude slower and uses infinitely more memory (equality doesn't use memory, serialization uses a lot). What you ask for is already available through SequenceEquals Commented Aug 10, 2023 at 7:10
  • 2
    Yes, because ... where's the equality comparer for Person? You get false if you compare two Person instances with the same values because the default comparer for classes only uses reference equality. Not so with records or structs. You can provide a custom comparer to SequenceEquals though Commented Aug 10, 2023 at 7:13

2 Answers 2

5

You need to use IEquatable Interface for this.

public class Person : IEquatable<Person> { public string Name { get; set; } public int Age { get; set; } public Person(string name, int age) { this.Name = name; this.Age = age; } public bool Equals(Person other) { if (other is null) return false; return this.Name == other.Name && this.Age == other.Age; } public override bool Equals(object obj) => Equals(obj as Person); public override int GetHashCode() => (Name, Age).GetHashCode(); } 

Then you can use SequenceEqual() method from System.Linq

var list1 = new List<Person>(); list1.Add(new Person("Dim", 12)); list1.Add(new Person("Dio", 13)); var list2 = new List<Person>(); list2.Add(new Person("Dim", 12)); list2.Add(new Person("Dio", 13)); bool test = list1.SequenceEqual(list2); Console.WriteLine(test); 
Sign up to request clarification or add additional context in comments.

1 Comment

... or simply change Person from a class to a record and get value-based comparison out-of-the-box
1

In C#, when you use the == operator to compare two lists, it will check if the two lists reference the same object in memory, not if their contents are the same.

use the SequenceEqual method from the System.Linq namespace.

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sequenceequal?view=net-7.0

3 Comments

To use SequenceEqual there must be an equality comparer for Person AND the list must be sorted (same order of elements).
@PoulBak nothing was said about order. The question asked how to compare two lists for equality. Not check if they have the same elements. Besides, both SequenceEqual and any methods that check for differences accept custom comparers
@PanagiotisKanavos: Ok, I phrased it badly. I meant that it will return false if it contains the same elements in a different order.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.