String::GetHashCode Method
Updated: February 2009
Returns the hash code for this string.
Assembly: mscorlib (in mscorlib.dll)
The behavior of GetHashCode is dependent on its implementation, which might change from one version of the common language runtime to another. A reason why this might happen is to improve the performance of GetHashCode.
Note: |
|---|
If two string objects are equal, the GetHashCode method returns identical values. However, there is not a unique hash code value for each unique string value. Different strings can return the same hash code. |
The value returned by GetHashCode is platform-dependent. For a specific string value, it differs on the 32-bit and 64-bit versions of the .NET Framework.
The following example demonstrates the GetHashCode method using various input strings.
using namespace System; void DisplayHashCode( String^ Operand ) { int HashCode = Operand->GetHashCode(); Console::WriteLine( "The hash code for \"{0}\" is: 0x{1:X8}, {1}", Operand, HashCode ); } int main() { DisplayHashCode( "" ); DisplayHashCode( "a" ); DisplayHashCode( "ab" ); DisplayHashCode( "abc" ); DisplayHashCode( "abd" ); DisplayHashCode( "abe" ); DisplayHashCode( "abcdef" ); DisplayHashCode( "abcdeg" ); DisplayHashCode( "abcdeh" ); DisplayHashCode( "abcdei" ); DisplayHashCode( "Abcdeg" ); DisplayHashCode( "Abcdeh" ); DisplayHashCode( "Abcdei" ); } /* The example displays output like the following: The hash code for "" is: 0x2D2816FE, 757602046 The hash code for "a" is: 0xCDCAB7BF, -842352705 The hash code for "ab" is: 0xCDE8B7BF, -840386625 The hash code for "abc" is: 0x2001D81A, 536991770 The hash code for "abd" is: 0xC2A94CB5, -1029092171 The hash code for "abe" is: 0x6550C150, 1699791184 The hash code for "abcdef" is: 0x1762906D, 392335469 The hash code for "abcdeg" is: 0x1763906D, 392401005 The hash code for "abcdeh" is: 0x175C906D, 391942253 The hash code for "abcdei" is: 0x175D906D, 392007789 The hash code for "Abcdeg" is: 0x1763954D, 392402253 The hash code for "Abcdeh" is: 0x175C954D, 391943501 The hash code for "Abcdei" is: 0x175D954D, 392009037 */
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, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
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.
Note: