Rich
Rich

Reputation: 3821

Which hash function does GetHashCode use?

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?

Upvotes: 1

Views: 1819

Answers (3)

SLaks
SLaks

Reputation: 887195

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.

Upvotes: 3

Guffa
Guffa

Reputation: 700152

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.

Upvotes: 3

RJ Lohan
RJ Lohan

Reputation: 6527

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.

Upvotes: 0

Related Questions