Object.GetHashCode Method
Assembly: mscorlib (in mscorlib.dll)
This method can be overridden by a derived class. Value classes must override this method to provide a hash function that is appropriate for the class and that ensures a better distribution in the hash table. Classes that might be used as a key in a hash table must also override this method, because objects that are used as keys in a hash table are required to generate their own hash code through this method. However, if the objects that are used as keys do not provide a useful implementation of GetHashCode, you can provide a different hash code provider, that is based on the System.Collections.IHashCodeProvider interface, when the Hashtable is constructed.
The default implementation of GetHashCode does not guarantee uniqueness or consistency; therefore, it must not be used as a unique object identifier for hashing purposes. Derived classes must override GetHashCode with an implementation that returns a unique hash code. For best results, the hash code must be based on the value of an instance field or property, instead of a static field or property.
Notes to Implementers A hash function is used to quickly generate a number (hash code) that corresponds to the value of an object. Hash functions are usually specific to each Type and must use at least one of the instance fields as input. A hash function must have the following properties:-
If two objects of the same type represent the same value, the hash function must return the same constant value for either object.
-
For the best performance, a hash function must generate a random distribution for all input.
-
The hash function must return exactly the same value regardless of any changes that are made to the object.
In some cases, GetHashCode is implemented to simply return an integer value. The following code example illustrates an implementation of GetHashCode, which returns an integer value.
import System.*; public class Int32 { public int value; //other methods... public int GetHashCode() { return value; } //GetHashCode } //Int32
Frequently, a type has multiple data fields that can participate in generating the hash code. One way to generate a hash code is to combine these fields using an XOR (eXclusive OR) operation, as shown in the following code example.
The following code example illustrates another case where the type's fields are combined using XOR (eXclusive OR) to generate the hash code. Notice that in this code example, the fields represent user-defined types, each of which implements GetHashCode and Equals.
If the data member of the derived class is bigger than an Int32, you can combine the high order bits of the value with the low order bits using an XOR (eXclusive OR) operation, as shown in the following code example.
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.