This documentation is archived and is not being maintained.

RuntimeHelpers::GetHashCode Method (Object)

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

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

public:
static int GetHashCode(
	Object^ o
)

Parameters

o
Type: System::Object

An Object to retrieve the hash code for.

Return Value

Type: System::Int32
A hash code for the Object identified by the o parameter.

The RuntimeHelpers::GetHashCode 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 differs from calling GetHashCode directly on the object with the Object::GetHashCode method.

The Object::GetHashCode and RuntimeHelpers::GetHashCode methods are used in the following scenarios:

String Interning

The CLR maintains an internal pool of strings and stores literals in it. If two strings (for example, str1 and str2) are identical, the CLR will set str1 and str2 to point to the same location on the managed heap to conserve memory. Calling RuntimeHelpers::GetHashCode 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. Therefore, if str2 was created as the result of a concatenation operation, and str2 is identical to str1, using RuntimeHelpers::GetHashCode 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 method.

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

The following code example demonstrates how to use the RuntimeHelpers::GetHashCode method to generate a hash code.

No code example is currently available or this language may not be supported.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1
Show: