请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
.NET Framework
Object 类
Object 方法
 GetHashCode 方法
全部折叠/全部展开 全部折叠
此页面仅适用于
Microsoft Visual Studio 2008/.NET Framework 3.5

同时提供下列产品的其他版本:
.NET Framework 类库
Object..::.GetHashCode 方法

更新:2007 年 11 月

用作特定类型的哈希函数。

命名空间:  System
程序集:  mscorlib(在 mscorlib.dll 中)
Visual Basic(声明)
Public Overridable Function GetHashCode As Integer
Visual Basic (用法)
Dim instance As Object
Dim returnValue As Integer

returnValue = instance.GetHashCode()
C#
public virtual int GetHashCode()
Visual C++
public:
virtual int GetHashCode()
J#
public int GetHashCode()
JScript
public function GetHashCode() : int

返回值

类型:System..::.Int32
当前 Object 的哈希代码。

GetHashCode 方法适用于哈希算法和诸如哈希表之类的数据结构。

GetHashCode 方法的默认实现不保证针对不同的对象返回唯一值。而且,.NET Framework 不保证 GetHashCode 方法的默认实现以及它所返回的值在不同版本的 .NET Framework 中是相同的。因此,在进行哈希运算时,该方法的默认实现不得用作唯一对象标识符。

GetHashCode 方法可以由派生类型重写。值类型必须重写此方法,以提供适合该类型的哈希函数和在哈希表中提供有用的分布。为了获得最佳结果,哈希代码必须基于实例字段或属性(而非静态字段或属性)的值。

用作 Hashtable 对象中键的对象还必须重写 GetHashCode 方法,因为这些对象必须生成其各自的哈希代码。如果用作键的对象不提供 GetHashCode 的有用实现,您可以在构造 Hashtable 对象时指定哈希代码提供程序。在 .NET Framework 2.0 版之前,哈希代码提供程序是基于 System.Collections..::.IHashCodeProvider 接口的。从 2.0 版开始,哈希代码提供程序基于 System.Collections..::.IEqualityComparer 接口。

对实现者的说明:

哈希函数用于快速生成一个与对象的值相对应的数字(哈希代码)。哈希函数通常是特定于每个 Type 的,而且,必须至少使用一个实例字段作为输入。

哈希函数必须具有以下特点:

  • 如果两个对象的比较结果相等,则每个对象的 GetHashCode 方法都必须返回同一个值。但是,如果两个对象的比较结果不相等,则这两个对象的 GetHashCode 方法不一定返回不同的值。

  • 一个对象的 GetHashCode 方法必须总是返回同一个哈希代码,但前提是没有修改过对象状态,对象状态用来确定对象的 Equals 方法的返回值。请注意,这仅适用于应用程序的当前执行,再次运行该应用程序时可能会返回另一个哈希代码。

  • 为了获得最佳性能,哈希函数必须为所有输入生成随机分布。

例如,String 类提供的 GetHashCode 方法的实现为相同的字符串值返回相同的哈希代码。因此,如果两个 String 对象表示相同的字符串值,则它们返回相同的哈希代码。另外,该方法使用字符串中的所有字符生成相当随机的分布式输出,即使当输入集中在某些范围内时(例如,许多用户可能有只包含低位 128 个 ASCII 字符的字符串,即使字符串可以包含 65,535 个 Unicode 字符中的任何字符)。

对于 Object 的派生类,当且仅当此派生类将值相等性定义为引用相等并且类型不是值类型时,GetHashCode 方法才可以委托给 Object..::.GetHashCode 实现。

在类上提供好的哈希函数可以显著影响将这些对象添加到哈希表的性能。在具有好的哈希函数实现的哈希表中,搜索元素所用的时间是固定的(例如运算复杂度为 O(1) 的运算)。而在具有不好的哈希函数实现的哈希表中,搜索性能取决于哈希表中的项数(例如运算复杂度为 O(n) 的运算,其中的 n 是哈希表中的项数)。哈希函数的计算成本也必须不高。

GetHashCode 方法的实现必须不会导致循环引用。例如,如果 ClassA.GetHashCode 调用 ClassB.GetHashCodeClassB.GetHashCode 必须不直接或间接调用 ClassA.GetHashCode

GetHashCode 方法的实现必须不引发异常。

重写 GetHashCode 的派生类还必须重写 Equals,以保证被视为相等的两个对象具有相同的哈希代码;否则,Hashtable 类型可能无法正常工作。

在某些情况下,GetHashCode 方法的实现只返回整数值。下面的代码示例阐释了返回整数值的 GetHashCode 的实现。

Visual Basic
Imports System

Public Structure Int32
    Public value As Integer

    'other methods...
    Public Overrides Function GetHashCode() As Integer 
        Return value
    End Function 'GetHashCode
End Structure 'Int32

C#
using System;

public struct Int32 {
   public int value;

   //other methods...

   public override int GetHashCode() {
      return value;
   }
}
Visual C++
using namespace System;
public value struct Int32
{
public:
   int value;

   //other methods...
   virtual int GetHashCode() override
   {
      return value;
   }

};
J#
import System.*;
public class Int32
{
    public int value;

    //other methods...
    public int GetHashCode()
    {
        return value;
    } //GetHashCode
} //Int32

一个类型常具有多个可以参与生成哈希代码的数据字段。生成哈希代码的一种方法是使用 XOR (eXclusive OR) 运算合并这些字段,如下面的代码示例所示。

Visual Basic
Imports System

Public Structure Point
    Public x As Integer
    Public y As Integer

    'other methods
    Public Overrides Function GetHashCode() As Integer 
        Return x XOr y
    End Function 
End Structure 
C#
using System;

public struct Point {
   public int x;
   public int y; 

   //other methods

   public override int GetHashCode() {
      return x ^ y;
   }
}
Visual C++
using namespace System;
public value struct Point
{
public:
   int x;
   int y;

   //other methods
   virtual int GetHashCode() override
   {
      return x ^ y;
   }

};

下面的代码示例阐释了另一种情况:使用 XOR (eXclusive OR) 合并该类型的字段以生成哈希代码。注意,在该代码示例中,字段表示用户定义的类型,每个类型都实现 GetHashCodeEquals

Visual Basic
Imports System

Public Class SomeType
    Public Overrides Function GetHashCode() As Integer 
        Return 0
    End Function 'GetHashCode
End Class 'SomeType

Public Class AnotherType
    Public Overrides Function GetHashCode() As Integer 
        Return 1
    End Function 'GetHashCode
End Class 'AnotherType

Public Class LastType
    Public Overrides Function GetHashCode() As Integer 
        Return 2
    End Function 'GetHashCode
End Class 'LastType

Public Class [MyClass]
    Private a As New SomeType()
    Private b As New AnotherType()
    Private c As New LastType()

    Public Overrides Function GetHashCode() As Integer 
        Return a.GetHashCode() ^ b.GetHashCode() ^ c.GetHashCode()
    End Function 'GetHashCode
End Class '[MyClass]
C#
using System;

public class SomeType {
   public override int GetHashCode() {
     return 0;
   }
}

public class AnotherType {
   public override int GetHashCode() {
     return 1;
   }
}

public class LastType {
   public override int GetHashCode() {
     return 2;
   }
}

public class MyClass {
   SomeType a = new SomeType();
   AnotherType b = new AnotherType();
   LastType c = new LastType();

   public override int GetHashCode () {
     return a.GetHashCode() ^ b.GetHashCode() ^ c.GetHashCode();
   }
}
Visual C++
using namespace System;
public ref class SomeType
{
public:
   virtual int GetHashCode() override
   {
      return 0;
   }

};

public ref class AnotherType
{
public:
   virtual int GetHashCode() override
   {
      return 1;
   }

};

public ref class LastType
{
public:
   virtual int GetHashCode() override
   {
      return 2;
   }

};

public ref class MyClass
{
private:
   SomeType^ a;
   AnotherType^ b;
   LastType^ c;

public:
   virtual int GetHashCode() override
   {
      return a->GetHashCode() ^ b->GetHashCode() ^ c->GetHashCode();
   }

};
JScript
import System;

public class SomeType {
   public override function GetHashCode(): int  {
     return 0;
   }
}

public class AnotherType {
   public override function GetHashCode(): int {
     return 1;
   }
}

public class LastType {
   public override function GetHashCode(): int {
     return 2;
   }
}

public class MyClass {
   var a: SomeType  = new SomeType();
   var b: AnotherType = new AnotherType();
   var c: LastType = new LastType();

   public override function GetHashCode () : int {
     return a.GetHashCode() ^ b.GetHashCode() ^ c.GetHashCode();
   }
}

如果派生类的数据成员比 Int32 大,则可以使用 XOR (eXclusive OR) 运算合并该值的高序位和低序位,如下面的代码示例所示。

Visual Basic
Imports System

Public Structure Int64
    Public value As Long

    'other methods...
    Public Overrides Function GetHashCode() As Integer 
        Return (Fix(value) ^ Fix(value >> 32))
    End Function 'GetHashCode
End Structure 'Int64
C#
using System;

public struct Int64 {
   public long value;

   //other methods...

   public override int GetHashCode() {
      return ((int)value ^ (int)(value >> 32));
   }
}
Visual C++
using namespace System;
public value struct Int64
{
public:
   long value;

   //other methods...

   virtual int GetHashCode() override
   {
      return ((int)value ^ (int)(value >> 32));
   }
};
JScript
import System;

public class Int64 {
   var value : long;

   //other methods...

   public override function GetHashCode() : int {
      return (int(value) ^ int(value >> 32));
   }   

   function Int64(myValue : long) {
      value = myValue;
   }
}

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求

.NET Framework

受以下版本支持:3.5、3.0、2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:3.5、2.0、1.0

XNA Framework

受以下版本支持:2.0、1.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利 | 商标 | 隐私权声明
Page view tracker