I have a class Person, it implements the Equals () method from IEquatable<Person>(also overrides the method Object.Equals, now ignores the GetHashcode () method)
class Person : IEquatable<Person>
{
public string Name { get; set; }
public bool Equals(Person other)
{
return this.Name == other.Name;
}
public override bool Equals(object obj)
{
var person = obj as Person;
return person != null && person.Name == Name;
}
}
Ok, let's get started:
Person p1 = new Person() { Name = "a" };
Person p2 = new Person() { Name = "a" };
List<Person> lst1 = new List<Person>() { p1 };
List<Person> lst2 = new List<Person>() { p2 };
Let's talk about this line:
bool b = lst1.SequenceEqual(lst2, EqualityComparer<Person>.Default);
I have a problem understanding this part:
EqualityComparer<Person>.Default
I heard that it EqualityComparer<Person>.Defaultchecks to see if the class implements IEquatable- it will take a method Equals(Person other), not Equals(object obj). he has the advantage of avoiding boxing
but
Equals(Person other)will run with or without output EqualityComparer<Person>.Default
(because it implements IEquatable)
So what are we talking about boxing? not there!
The only time it Equals(object obj)will be launched will be:
bool b = lst1.SequenceEqual(lst2,EqualityComparer<Object>.Default);
! object, Person!
? EqualityComparer<Object>.Default. -, , , , ?