Updated: February 2009
Returns the hash code for this string.
Namespace:
System
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Overrides Function GetHashCode As Integer
Dim instance As String
Dim returnValue As Integer
returnValue = instance.GetHashCode()
public override int GetHashCode()
public:
virtual int GetHashCode() override
public override function GetHashCode() : int
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. |
Notes to Callers: The value returned by GetHashCode is platform-dependent. The value returned by the GetHashCode method for a particular string value differs on the 32-bit and 64-bit versions of the .NET Framework.
The following example demonstrates the GetHashCode method using various input strings.
' Example for the String.GetHashCode( ) method.
Imports System
Imports Microsoft.VisualBasic
Module GetHashCode
Sub Main()
Console.WriteLine( _
"This example of String.GetHashCode( ) " & _
"generates the following output." & vbCrLf)
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")
End Sub 'Main
Sub DisplayHashCode(Operand As String)
Dim HashCode As Integer = Operand.GetHashCode()
Console.WriteLine( _
"The hash code for ""{0}"" is: 0x{1:X8}, {1}", _
Operand, HashCode)
End Sub 'DisplayHashCode
End Module 'GetHashCode
' This example of String.GetHashCode( ) generates the following output.
'
' The hash code for "" is: 0x00001505, 5381
' The hash code for "a" is: 0x0002B5C4, 177604
' The hash code for "ab" is: 0x00596E26, 5860902
' The hash code for "abc" is: 0x0B873285, 193409669
' The hash code for "abd" is: 0x0B873282, 193409666
' The hash code for "abe" is: 0x0B873283, 193409667
' The hash code for "abcdef" is: 0x4DDB4BE2, 1306217442
' The hash code for "abcdeg" is: 0x4DDB4BE3, 1306217443
' The hash code for "abcdeh" is: 0x4DDB4BEC, 1306217452
' The hash code for "abcdei" is: 0x4DDB4BED, 1306217453
' The hash code for "Abcdeg" is: 0x941C4FC3, -1810083901
' The hash code for "Abcdeh" is: 0x941C4FCC, -1810083892
' The hash code for "Abcdei" is: 0x941C4FCD, -1810083891
// Example for the String.GetHashCode( ) method.
using System;
class GetHashCode
{
public static void Main()
{
Console.WriteLine(
"This example of String.GetHashCode( ) " +
"generates the following output.\n" );
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" );
}
static void DisplayHashCode( String Operand )
{
int HashCode = Operand.GetHashCode( );
Console.WriteLine(
"The hash code for \"{0}\" is: 0x{1:X8}, {1}",
Operand, HashCode );
}
}
/*
This example of String.GetHashCode( ) generates the following output.
The hash code for "" is: 0x00001505, 5381
The hash code for "a" is: 0x0002B5C4, 177604
The hash code for "ab" is: 0x00596E26, 5860902
The hash code for "abc" is: 0x0B873285, 193409669
The hash code for "abd" is: 0x0B873282, 193409666
The hash code for "abe" is: 0x0B873283, 193409667
The hash code for "abcdef" is: 0x4DDB4BE2, 1306217442
The hash code for "abcdeg" is: 0x4DDB4BE3, 1306217443
The hash code for "abcdeh" is: 0x4DDB4BEC, 1306217452
The hash code for "abcdei" is: 0x4DDB4BED, 1306217453
The hash code for "Abcdeg" is: 0x941C4FC3, -1810083901
The hash code for "Abcdeh" is: 0x941C4FCC, -1810083892
The hash code for "Abcdei" is: 0x941C4FCD, -1810083891
*/
// Example for the String::GetHashCode( ) method.
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()
{
Console::WriteLine( "This example of String::GetHashCode( ) "
"generates the following output.\n" );
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" );
}
/*
This example of String::GetHashCode( ) generates the following output.
The hash code for "" is: 0x00001505, 5381
The hash code for "a" is: 0x0002B5C4, 177604
The hash code for "ab" is: 0x00596E26, 5860902
The hash code for "abc" is: 0x0B873285, 193409669
The hash code for "abd" is: 0x0B873282, 193409666
The hash code for "abe" is: 0x0B873283, 193409667
The hash code for "abcdef" is: 0x4DDB4BE2, 1306217442
The hash code for "abcdeg" is: 0x4DDB4BE3, 1306217443
The hash code for "abcdeh" is: 0x4DDB4BEC, 1306217452
The hash code for "abcdei" is: 0x4DDB4BED, 1306217453
The hash code for "Abcdeg" is: 0x941C4FC3, -1810083901
The hash code for "Abcdeh" is: 0x941C4FCC, -1810083892
The hash code for "Abcdei" is: 0x941C4FCD, -1810083891
*/
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.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0, 1.0
XNA Framework
Supported in: 3.0, 2.0, 1.0
Reference
Date | History | Reason |
|---|
February 2009
| Added Notes for Callers section. |
Content bug fix.
|
July 2008
| Added a note to the Remarks section about the uniqueness of values returned by the GetHashCode method. |
Content bug fix.
|