String.GetHashCode Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns the hash code for this string.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Overrides Function GetHashCode As Integer
[SecuritySafeCriticalAttribute]
public override int GetHashCode()

Return Value

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

Remarks

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.

Examples

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
// Example for the String.GetHashCode( ) method.
using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text +=
          "This example of String.GetHashCode() " +
          "generates the following output.\n" + "\n";

      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");
   }

   static void DisplayHashCode(System.Windows.Controls.TextBlock outputBlock, String Operand)
   {
      int HashCode = Operand.GetHashCode();
      outputBlock.Text += String.Format(
          "The hash code for \"{0}\" is: 0x{1:X8}, {1}",
          Operand, HashCode) + "\n";
   }
}

/*
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
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference