System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode Method

Serves as a hash function for a particular object, and is suitable for use in algorithms and data structures that use hash codes, such as a hash table.


public static int GetHashCode (object o)


An object to retrieve the hash code for.


A hash code for the object identified by the o parameter.


The RuntimeHelpers.GetHashCode(object) method always calls the object.GetHashCode method non-virtually, even if the object's type has overridden the object.GetHashCode method. Therefore, using RuntimeHelpers.GetHashCode(object) might differ from calling GetHashCode directly on the object with the object.GetHashCode method.


Although the RuntimeHelpers.GetHashCode(object) method returns identical hash codes for identical object references, you should not use this method to test for object identity, because this hash code does not uniquely identify an object reference. To test for object identify (that is, to test that two objects reference the same object in memory), call the object.ReferenceEquals(object, object) method. Nor should you use RuntimeHelpers.GetHashCode(object) to test whether two strings represent equal object references, because the string is interned. To test for string interning, call the string.IsInterned(string) method.

The object.GetHashCode and RuntimeHelpers.GetHashCode(object) methods differ as follows:

  • object.GetHashCode returns a hash code that is based on the object's definition of equality. For example, two strings with identical contents will return the same value for object.GetHashCode.

  • RuntimeHelpers.GetHashCode(object) returns a hash code that indicates object identity. That is, two string variables whose contents are identical and that represent a string that is interned (see the String Interning section) or that represent a single string in memory return identical hash codes.


Note that RuntimeHelpers.GetHashCode(object) always returns identical hash codes for equal object references. However, the reverse is not true: equal hash codes do not indicate equal object references. A particular hash code value is not unique to a particular object reference; different object references can generate identical hash codes.

This method is used by compilers.

String Interning

The common language runtime (CLR) maintains an internal pool of strings and stores literals in the pool. If two strings (for example, str1 and str2) are formed from an identical string literal, the CLR will set str1 and str2 to point to the same location on the managed heap to conserve memory. Calling RuntimeHelpers.GetHashCode(object) on these two string objects will produce the same hash code, contrary to the second bulleted item in the previous section.

The CLR adds only literals to the pool. Results of string operations such as concatenation are not added to the pool, unless the compiler resolves the string concatenation as a single string literal. Therefore, if str2 was created as the result of a concatenation operation, and str2 is identical to str1, using RuntimeHelpers.GetHashCode(object) on these two string objects will not produce the same hash code.

If you want to add a concatenated string to the pool explicitly, use the string.Intern(string) method.

You can also use the string.IsInterned(string) method to check whether a string has an interned reference.


Namespace: System.Runtime.CompilerServices
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0,,