Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.GetHashCode Method

Returns the hash code for this string.

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

[SecuritySafeCriticalAttribute]
public override int GetHashCode()

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.

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


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


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

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft