blog.jj5.net (2003 to 2005)

(Not) Implementing GetHashCode()

Thu Jun 10 21:12:00 UTC+1000 2004

Categories:

[Update: Short story longer. I won't be doing this at all]

In .NET the base type System.Object (from which all types derive) defines a public virtual Int32 GetHashCode() method.

Now, if you override Equals() you need to override GetHashCode() too. Since GetHashCode() has to be consistent, I always thought that if you wanted to implement Equals() then you'd need to make your type immutable so as to be able to generate a reasonable hash code.

Long story short, my code will be seeing a few methods that look like this shortly:

public override Int32 GetHashCode() {

  return 0x25;

}

;)

John.


Copyright © 2003-2005 John Elliot