Started with overriding concepts and I override the methods Equals
and GetHashCode
.
Primarily I came up with this "very simple code":
internal class Person
{
public string name;
public int age;
public string lname;
public Person(string name, int age, string lname)
{
this.name = name;
this.age = age;
this.lname = lname;
}
public override bool Equals(object obj)
{
var person = obj as Person;
if (person != null)
{
return person.age == this.age && person.name == this.name && person.lname == this.lname;
}
return false;
}
public override int GetHashCode()
{
return this.age.GetHashCode() * this.name.GetHashCode() * this.lname.GetHashCode();
}
}
While this works great, my "co-developer" Mr.Resharper gave me some suggestions:
- Non-readonly fields referenced in GetHashCode(). Suggestions came in this line of code:
return this.age.GetHashCode() * this.name.GetHashCode() * this.lname.GetHashCode();
- Should we use GetHashCode only for Properties?