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

TimeSpan.GetHashCode 方法

2013/12/13

返回此实例的哈希代码。

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

public override int GetHashCode()

返回值

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

即使两个 TimeSpan 对象表示不同的时间值,它们也可能具有相同的哈希代码。

下面的代码示例使用 GetHashCode 方法生成几个 TimeSpan 对象的哈希代码。


// Example for the TimeSpan.GetHashCode( ) method.
using System;

class Example
{
   static void DisplayHashCode(System.Windows.Controls.TextBlock outputBlock, TimeSpan interval)
   {
      // Create a hash code and a string representation of 
      // the TimeSpan parameter.
      string timeInterval = interval.ToString();
      int hashCode = interval.GetHashCode();

      // Pad the end of the TimeSpan string with spaces if it 
      // does not contain milliseconds.
      int pIndex = timeInterval.IndexOf(':');
      pIndex = timeInterval.IndexOf('.', pIndex);
      if (pIndex < 0) timeInterval += "        ";

      outputBlock.Text += String.Format("{0,22}   0x{1:X8}, {1}",
          timeInterval, hashCode) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += String.Format(
          "This example of TimeSpan.Example( ) generates " +
          "the following \noutput, which displays " +
          "the hash codes of representative TimeSpan \n" +
          "objects in hexadecimal and decimal formats.\n") + "\n";
      outputBlock.Text += String.Format("{0,22}   {1,10}",
          "TimeSpan        ", "Hash Code") + "\n";
      outputBlock.Text += String.Format("{0,22}   {1,10}",
          "--------        ", "---------") + "\n";

      DisplayHashCode(outputBlock, new TimeSpan(0));
      DisplayHashCode(outputBlock, new TimeSpan(1));
      DisplayHashCode(outputBlock, new TimeSpan(0, 0, 0, 0, 1));
      DisplayHashCode(outputBlock, new TimeSpan(0, 0, 1));
      DisplayHashCode(outputBlock, new TimeSpan(0, 1, 0));
      DisplayHashCode(outputBlock, new TimeSpan(1, 0, 0));
      DisplayHashCode(outputBlock, new TimeSpan(36000000001));
      DisplayHashCode(outputBlock, new TimeSpan(0, 1, 0, 0, 1));
      DisplayHashCode(outputBlock, new TimeSpan(1, 0, 1));
      DisplayHashCode(outputBlock, new TimeSpan(1, 0, 0, 0));
      DisplayHashCode(outputBlock, new TimeSpan(864000000001));
      DisplayHashCode(outputBlock, new TimeSpan(1, 0, 0, 0, 1));
      DisplayHashCode(outputBlock, new TimeSpan(1, 0, 0, 1));
      DisplayHashCode(outputBlock, new TimeSpan(100, 0, 0, 0));
      DisplayHashCode(outputBlock, new TimeSpan(100, 0, 0, 0, 1));
      DisplayHashCode(outputBlock, new TimeSpan(100, 0, 0, 1));
   }
}

/*
This example of TimeSpan.GetHashCode( ) generates the following
output, which displays the hash codes of representative TimeSpan
objects in hexadecimal and decimal formats.

      TimeSpan            Hash Code
      --------            ---------
      00:00:00           0x00000000, 0
      00:00:00.0000001   0x00000001, 1
      00:00:00.0010000   0x00002710, 10000
      00:00:01           0x00989680, 10000000
      00:01:00           0x23C34600, 600000000
      01:00:00           0x61C46808, 1640261640
      01:00:00.0000001   0x61C46809, 1640261641
      01:00:00.0010000   0x61C48F18, 1640271640
      01:00:01           0x625CFE88, 1650261640
    1.00:00:00           0x2A69C0C9, 711573705
    1.00:00:00.0000001   0x2A69C0C8, 711573704
    1.00:00:00.0010000   0x2A69E7D9, 711583705
    1.00:00:01           0x2B025649, 721573449
  100.00:00:00           0x914F4E94, -1857073516
  100.00:00:00.0010000   0x914F6984, -1857066620
  100.00:00:01           0x91E7D814, -1847076844
*/


Windows Phone OS

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

Windows Phone

显示: