信息
您所需的主题如下所示。但此主题未包含在此库中。

String.GetHashCode 方法

2013/12/13

返回该字符串的哈希代码。

Namespace:  System
程序集:  mscorlib(位于 mscorlib.dll 中)

public override int GetHashCode()

返回值

类型: System.Int32
32 位有符号整数哈希代码。

GetHashCode 的行为取决于它的实现,此实现可能会从一个公共语言运行时版本更改为另一个版本。原因可能是为了提高 GetHashCode 的性能。

说明注意:

如果两个字符串对象相等,则 GetHashCode 方法返回相同的值。但是,每个唯一的字符串值并没有唯一的哈希代码值。不同的字符串可以返回相同的哈希代码。有关哈希代码的更多信息,请参见 Object.GetHashCode 参考主题。

下面的代码示例使用各种输入字符串演示 GetHashCode 方法。


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


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

显示: