1

I've made the assumption that the (generic) Dictionary class in .NET uses the GetHashCode() method on its keys to produce hashes. I have two questions leading on from it:

  1. Object has an overridable GetHashCode() method. For a user defined reference type object, will this method produce a hash based on the referenced data? e.g. If I have a class OneString which contains only one String instance variable - will two separate instances of this class with matching strings always produce the same hash code? Or does the GetHashCode() method of OneString need to be overridden to achieve this functionality?

  2. Presumably the hash function implemented in the String class is different to the hash function implemented in a different reference type (e.g. BitmapImage). Are the hash functions implemented in the most common classes publicly available?

Rich
  • 3,781
  • 5
  • 34
  • 56
  • Check out http://stackoverflow.com/questions/1008633/gethashcode-problem-using-xor for discussion on common ways to implement GetHashCode – Alexei Levenkov May 29 '12 at 23:13
  • Check out this similar question: [Default implementation for Object.GetHashCode()](http://stackoverflow.com/questions/720177/default-implementation-for-object-gethashcode) – Thomas C. G. de Vilhena May 29 '12 at 23:14
  • You don't really care about what the hash function *is*, from a usage standpoint. You just care that it satisfies the properties of a hash function. (After all, if it were predictable, then it wouldn't be a good hash function. :P) – user541686 May 29 '12 at 23:36

3 Answers3

3

No.

object.GetHashCode() returns a value based on that object's identity alone.
It will not return the same value for two equivalent objects; it is completely unaware of the type or meaning of the object.

Classes that represent values (such as String) override GetHashCode() to return a hash based on the value represented.
The algorithm used is up to the class designer; GetHashCode() is written like any other method.
However, GetHashCode() is supposed to return equal values whenever Equals() returns true; if your class does not do this, it is wrong.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Not actually true... http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx "If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values." – Jay May 29 '12 at 23:12
  • @Jay: That is speaking to people who override `GetHashCode()`. If your class overrides `Equals()` but not `GetHashCode()`, that will not be true (and your class will be broken) – SLaks May 29 '12 at 23:12
  • @SLaks: The value returned by `Object.GetHashCode` method doesn't "depend on the object's memory address alone". Object's memory addresses will usually change during a garbage collection, but their hash codes never will. Instead, each object has a 32-bit or 64-bit word which, depending upon some of the bits therein, will either hold the object's hash code in some of its bits, or else contain an index to a structure called a sync record which has 32 bits allocated for the hash code; in any case, the "hash code" is a number that's made up the first time `GetHashCode` is called on an object. – supercat May 29 '12 at 23:20
  • "object.GetHashCode() returns a value based on that object's memory address alone." This probably has to be qualified with the version/platform of the runtime used. I don't believe that being based on memory address is a part of the spec. – Andrew Savinykh May 29 '12 at 23:23
  • @SLaks if you override GetHashCode and not Equals you will get a Warning :P – Jay May 30 '12 at 03:29
3

Object has an overridable GetHashCode() method. For a user defined reference type object, will this method produce a hash based on the referenced data?

No, the default GetHashCode method doesn't attempt to use the data in the class, it only bases it on the reference. Two separate instances with identical content will have different hash codes.

If I have a class OneString which contains only one String instance variable - will two separate instances of this class with matching strings always produce the same hash code? Or does the GetHashCode() method of OneString need to be overridden to achieve this functionality?

You have to override it.

Presumably the hash function implemented in the String class is different to the hash function implemented in a different reference type (e.g. SqlCommand). Are the hash functions implemented in the most common classes publicly available?

Yes, the GetHashCode for strings and common value types are implemented to produce a working hash code from the values.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

1) Different string instances with the same contents will always produce the same hash code. (see: http://msdn.microsoft.com/en-us/library/system.string.gethashcode.aspx)

2) GetHashCode() is a method of the base Object class, from which all types derive. So, there is always an implementation of this method for any type.

RJ Lohan
  • 6,497
  • 3
  • 34
  • 54