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);
Personclass does not override theEqualsmethod and==only checks reference-equality, you want to callobject.EqualsorEqualson your listPerson? You getfalseif 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 toSequenceEqualsthough