Given the following class
public class Foo { public int FooId { get; set; } public string FooName { get; set; } public override bool Equals(object obj) { Foo fooItem = obj as Foo; if (fooItem == null) { return false; } return fooItem.FooId == this.FooId; } public override int GetHashCode() { // Which is preferred? return base.GetHashCode(); //return this.FooId.GetHashCode(); } }
I have overridden the Equals method because Foo represent a row for the Foos table. Which is the preferred method for overriding the GetHashCode?
Why is it important to override GetHashCode?
Yes, it is important if your item will be used as a key in a dictionary, or
HashSet<T>, etc – since this is used (in the absence of a customIEqualityComparer<T>) to group items into buckets. If the hash-code for two items does not match, they may never be considered equal (Equals will simply never be called).The GetHashCode() method should reflect the
Equalslogic; the rules are:Equals(...) == true) then they must return the same value forGetHashCode()GetHashCode()is equal, it is not necessary for them to be the same; this is a collision, andEqualswill be called to see if it is a real equality or not.In this case, it looks like "
return FooId;" is a suitableGetHashCode()implementation. If you are testing multiple properties, it is common to combine them using code like below, to reduce diagonal collisions (i.e. so thatnew Foo(3,5)has a different hash-code tonew Foo(5,3)):In modern frameworks, the
HashCodetype has methods to help you create a hashcode from multiple values; on older frameworks, you’d need to go without, so something like:Oh – for convenience, you might also consider providing
==and!=operators when overridingEqualsandGetHashCode.A demonstration of what happens when you get this wrong is here.