RuntimeHelpers.GetHashCode Method (Object)
Assembly: mscorlib (in mscorlib.dll)
public static int GetHashCode ( Object o )
public static function GetHashCode ( o : Object ) : int
Not applicable.
Parameters
- o
An Object to retrieve the hash code for.
Return Value
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:
-
Object.GetHashCode is useful in scenarios where you care about object value. Two strings with identical contents will return the same value for Object.GetHashCode.
-
RuntimeHelpers.GetHashCode is useful in scenarios where you care about object identity. Two strings with identical contents will return different values for RuntimeHelpers.GetHashCode, because they are different string objects, although their contents are the same.
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.
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.