NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Object.GetHashCode

Serves as a hash function for a particular type, suitable for use in hashing algorithms and data structures like a hash table.

[Visual Basic]
Overridable Public Function GetHashCode() As Integer
[C#]
public virtual int GetHashCode();
[C++]
public: virtual int GetHashCode();
[JScript]
public function GetHashCode() : int;

Return Value

A hash code for the current Object.

Remarks

This method can be overridden by a derived class. Value classes and classes that may be used as a key in a hash table must override this method.

The return value from GetHashCode must not be persisted for two reasons. First, the hash function of a class might be altered by its implementer to generate a better distribution, rendering any values from the old hash function useless. Second, the default implementation of Object does not guarantee that the same value will be returned by different instances.

Notes to Implementers: 

A hash function is used to quickly generate a number (hash code) corresponding to the value of an object. Hash functions usually are specific for each type, and should use at least one of the instance fields as input. A hash function has two interesting properties:

Ensuring these properties are met is up to the implementor of the GetHashCode method. Object's GetHashCode does not know anything about the values represented by specific objects, so it can only guarantee that if you call GetHashCode on the same instance repeatedly, it will return the same value.

Providing a good hash function on a class can affect the performance of adding those objects to a hash table by an enormous amount. A good implementation will mean hash table lookups take constant time (ie, they are O(1)), while an extremely poor one that always returned a hard-coded number would make hash table lookups depend on the number of items in the hash table (ie, they would be O(n)). Hash functions should also be cheap to compute.

Subclass implementations of GetHashCode should guarantee the properties listed above. For example, the implementation of GetHashCode provided by the String class returns unique hash codes for unique string values. Therefore, two String objects will return the same hash code if and only if they represent the same string value. Also, the method uses all of the characters in the string to generate reasonably randomly distributed output, even when the input may be clustered in certain ranges (ie, many users may only have strings containing the lower 128 ASCII characters, though Unicode allows over 65,535 characters).

The default implementation returns an runtime-determined index for the object, which is unique to an instance of an object within an AppDomain for an instance of the EE. However, because this index can be reused after an object is garbage collected, it is possible to obtain the same hash code for two different objects. Also, two objects that represent the same value will have the same hash code only if they are the exact same object. This implementation is not particularly useful for hashing, and it is why subclasses should override GetHashCode.

See Also

Object Class | Object Members | System Namespace | Hashtable