Equals and GetHashCode
c#-3.0
Solution
A few points:
It's not null safe or "different type" safe. Try this:
new Person().Equals(new Object());
or
new Person().Equals(null);
Bang.
Classes defining equality operations should usually be immutable IMO. Changing the contents of an object after using it as a dictionary key is a Bad Thing, for example.
Consider implementing `IEquatable<Person>`
A quick reimplementation, which still assumes you want equality based solely on ID.
public sealed class Person : IEquatable<Person> {
private readonly int personId;
public int PersonId { get { return personId; }
private readonly string name;
public string Name { get { return name; } }
public Person(int personId, string name) {
// Is a null name valid? If not, throw here.
this.personId = personId;
this.name = name;
}
public override bool Equals(object obj) {
return Equals(obj as Person);
}
public Equals(Person other) {
return other != null && other.personId == personId;
}
public override int GetHashCode() {
return personId;
}
}
Problem
What do you think about this Person class? Is it a bad idea or best practise to override Equals and GetHashCode like that? ``` public class Person { public int PersonId { get; set; } public string Name { get; set; } public override bool Equals(object obj) { var person = obj as Person; return PersonId == person.PersonId; } public override int GetHashCode() { return PersonId; } } ``` Usage : ``` static void Main(string[] args) { var list = new List<Person>(); list.Add(new Person(){ PersonId = 1, Name = "Mike"}); list.Add(new Person() { PersonId = 2, Name = "Michael Sync" }); list.Add(new Person(){ PersonId = 1, Name = "Mike"}); var list1 = new List<Person>(); list1.Add(new Person() { PersonId = 1, Name = "Mike" }); list1.Add(new Person() { PersonId = 3, Name = "Julia" }); var except = list.Except(list1); foreach (var item in except) { Console.WriteLine(item.Name); } Console.ReadKey(); } ```