System.Object.GetHashCode Method

Serves as the default hash function.

Syntax

public virtual int GetHashCode ()

Returns

A hash code for the current object.

Remarks

A hash code is a numeric value that is used to insert and identify an object in a hash-based collection such as the Dictionary`2 class, the Hashtable class, or a type derived from the DictionaryBase class. The object.GetHashCode method provides this hash code for algorithms that need quick checks of object equality.

Note:

For information about how hash codes are used in hash tables and for some additional hash code algorithms, see the tp://en.wikipedia.org/wiki/Hash_function entry in Wikipedia.

Two objects that are equal return hash codes that are equal. However, the reverse is not true: equal hash codes do not imply object equality, because different (unequal) objects can have identical hash codes. Furthermore, the .NET Framework does not guarantee the default implementation of the object.GetHashCode method, and the value this method returns may differ between .NET Framework versions and platforms, such as 32-bit and 64-bit platforms. For these reasons, do not use the default implementation of this method as a unique object identifier for hashing purposes. Two consequences follow from this:

  • You should not assume that equal hash codes imply object equality.

  • You should never persist or use a hash code outside the application domain in which it was created, because the same object may hash across application domains, processes, and platforms.

Note:

A hash code is intended for efficient insertion and lookup in collections that are based on a hash table. A hash code is not a permanent value. For this reason:

  • Do not serialize hash code values or store them in databases.

  • Do not use the hash code as the key to retrieve an object from a keyed collection.

  • Do not send hash codes across application domains or processes. In some cases, hash codes may be computed on a per-process or per-application domain basis.

  • Do not use the hash code instead of a value returned by a cryptographic hashing function if you need a cryptographically strong hash. For cryptographic hashes, use a class derived from the System.Security.Cryptography.HashAlgorithm or System.Security.Cryptography.KeyedHashAlgorithm class.

  • Do not test for equality of hash codes to determine whether two objects are equal. (Unequal objects can have identical hash codes.) To test for equality, call the object.ReferenceEquals(object, object) or object.Equals(object) method.

The object.GetHashCode method can be overridden by a derived type. If object.GetHashCode is not overridden, hash codes for reference types are computed by calling the object.GetHashCode method of the base class, which computes a hash code based on an object's reference; for more information, see System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(object). In other words, two objects for which the object.ReferenceEquals(object, object) method returns true have identical hash codes. If value types do not override object.GetHashCode, the ValueType.GetHashCode method of the base class uses reflection to compute the hash code based on the values of the type's fields. In other words, value types whose fields have equal values have equal hash codes. For more information about overriding object.GetHashCode, see the "Notes to Inheritors" section.

Note:

If you override the object.GetHashCode method, you should also override object.Equals(object), and vice versa. If your overridden object.Equals(object) method returns true when two objects are tested for equality, your overridden object.GetHashCode method must return the same value for the two objects.

If an object that is used as a key in a hash table does not provide a useful implementation of object.GetHashCode, you can specify a hash code provider by supplying an IEqualityComparer implementation to one of the overloads of the Hashtable class constructor.

Notes for the wrt

When you call the object.GetHashCode method on a class in the wrt, it provides the default behavior for classes that don’t override object.GetHashCode. This is part of the support that the .NET Framework provides for the wrt (see .NET Framework Support for Windows Store Apps and Windows Runtime). Classes in the wrt don’t inherit object, and currently don’t implement a object.GetHashCode. However, they appear to have object.ToString, object.Equals(object), and object.GetHashCode methods when you use them in your C# or Visual Basic code, and the .NET Framework provides the default behavior for these methods.

Note:

wrt classes that are written in C# or Visual Basic can override the object.GetHashCode method.

Example

Example 1

In some cases, object.GetHashCode is implemented to simply return an integer value. The following example illustrates an implementation of int.GetHashCode , which returns an integer value:

C# Example

using System;
public struct Int32 {
 int value;
 //other methods...

 public override int GetHashCode() {
 return value;
 }
}

Example 2

Frequently, a type has multiple data members 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 example:

C# Example

using System;
public struct Point {
 int x;
 int y; 
 //other methods
 
 public override int GetHashCode() {
 return x ^ y;
 }
}

Example 3

The following example illustrates another case where the type's fields are combined using xor (exclusive or) to generate the hash code. Notice that in this example, the fields represent user-defined types, each of which implements object.GetHashCode (and should implement object.Equals(object) as well):

C# Example

using System;
public class SomeType {
 public override int GetHashCode() {
 return 0;
 }
}

public class AnotherType {
 public override int GetHashCode() {
 return 1;
 }
}

public class LastType {
 public override int GetHashCode() {
 return 2;
 }
}
public class MyClass {
 SomeType a = new SomeType();
 AnotherType b = new AnotherType();
 LastType c = new LastType();

 public override int GetHashCode () {
 return a.GetHashCode() ^ b.GetHashCode() ^ c.GetHashCode();
 }
}

Avoid implementing object.GetHashCode in a manner that results in circular references. In other words, if AClass.GetHashCode calls BClass.GetHashCode, it should not be the case that BClass.GetHashCode calls AClass.GetHashCode.

Example 4

In some cases, the data member of the class in which you are implementing object.GetHashCode is bigger than a int. In such cases, you could combine the high order bits of the value with the low order bits using an XOR operation, as shown in the following example:

C# Example

using System;
public struct Int64 {
 long value;
 //other methods...

 public override int GetHashCode() {
 return ((int)value ^ (int)(value >> 32));
 }
}

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0