EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.

String.GetHashCode Method

May 11, 2014

Returns the hash code for this string.

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

'Déclaration
Public Overrides Function GetHashCode As Integer

Return Value

Type: System.Int32
A 32-bit signed integer hash code.

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.

NoteNote:

   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. For more information about hash codes, see the Object.GetHashCode reference topic.

The following code example demonstrates the GetHashCode method using various input strings.


' Example for the String.GetHashCode( ) method.

Module Example

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text += _
          "This example of String.GetHashCode( ) " & _
          "generates the following output." & vbCrLf & vbCrLf

      DisplayHashCode(outputBlock, "")
      DisplayHashCode(outputBlock, "a")
      DisplayHashCode(outputBlock, "ab")
      DisplayHashCode(outputBlock, "abc")
      DisplayHashCode(outputBlock, "abd")
      DisplayHashCode(outputBlock, "abe")
      DisplayHashCode(outputBlock, "abcdef")
      DisplayHashCode(outputBlock, "abcdeg")
      DisplayHashCode(outputBlock, "abcdeh")
      DisplayHashCode(outputBlock, "abcdei")
      DisplayHashCode(outputBlock, "Abcdeg")
      DisplayHashCode(outputBlock, "Abcdeh")
      DisplayHashCode(outputBlock, "Abcdei")
   End Sub 'Main

   Sub DisplayHashCode(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal Operand As String)

      Dim HashCode As Integer = Operand.GetHashCode()
      outputBlock.Text += String.Format( _
          "The hash code for ""{0}"" is: 0x{1:X8}, {1}", _
          Operand, HashCode) & vbCrLf

   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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Afficher:
© 2014 Microsoft