Object.GetHashCode Object.GetHashCode Object.GetHashCode Object.GetHashCode Method

Definition

作为默认哈希函数。 Serves as the default hash function.

public:
 virtual int GetHashCode();
public virtual int GetHashCode ();
abstract member GetHashCode : unit -> int
override this.GetHashCode : unit -> int
Public Overridable Function GetHashCode () As Integer

Returns

当前对象的哈希代码。 A hash code for the current object.

Examples

若要计算具有相同或比范围较小的数字值的哈希代码的最简单方式之一Int32类型是只需返回该值。One of the simplest ways to compute a hash code for a numeric value that has the same or a smaller range than the Int32 type is to simply return that value. 下面的示例显示了此类实现Number结构。The following example shows such an implementation for a Number structure.

using System;

public struct Number
{
   private int n;

   public Number(int value)
   {
      n = value;
   }

   public int Value
   {
      get { return n; }
   }
   
   public override bool Equals(Object obj)
   {
      if (obj == null || ! (obj is Number)) 
         return false;
      else
         return n == ((Number) obj).n;
   }      
   
   public override int GetHashCode()
   {
      return n;
   }
   
   public override string ToString()
   {
      return n.ToString();
   }
}

public class Example
{
   public static void Main()
   {
      Random rnd = new Random();
      for (int ctr = 0; ctr <= 9; ctr++) {
         int randomN = rnd.Next(Int32.MinValue, Int32.MaxValue);
         Number n = new Number(randomN);
         Console.WriteLine("n = {0,12}, hash code = {1,12}", n, n.GetHashCode());
      }   
   }
}
// The example displays output like the following:
//       n =   -634398368, hash code =   -634398368
//       n =   2136747730, hash code =   2136747730
//       n =  -1973417279, hash code =  -1973417279
//       n =   1101478715, hash code =   1101478715
//       n =   2078057429, hash code =   2078057429
//       n =   -334489950, hash code =   -334489950
//       n =    -68958230, hash code =    -68958230
//       n =   -379951485, hash code =   -379951485
//       n =    -31553685, hash code =    -31553685
//       n =   2105429592, hash code =   2105429592
Public Structure Number
   Private n As Integer

   Public Sub New(value As Integer)
      n = value
   End Sub

   Public ReadOnly Property Value As Integer
      Get
         Return n
      End Get
   End Property
   
   Public Overrides Function Equals(obj As Object) As Boolean
      If obj Is Nothing OrElse Not TypeOf obj Is Number Then
         Return False
      Else
         Return n = CType(obj, Number).n
      End If
   End Function      
   
   Public Overrides Function GetHashCode() As Integer
      Return n
   End Function
   
   Public Overrides Function ToString() As String
      Return n.ToString()
   End Function
End Structure

Module Example
   Public Sub Main()
      Dim rnd As New Random()
      For ctr As Integer = 0 To 9
         Dim randomN As Integer = rnd.Next(Int32.MinValue, Int32.MaxValue)
         Dim n As New Number(randomN)
         Console.WriteLine("n = {0,12}, hash code = {1,12}", n, n.GetHashCode())
      Next
   End Sub
End Module
' The example displays output like the following:
'       n =   -634398368, hash code =   -634398368
'       n =   2136747730, hash code =   2136747730
'       n =  -1973417279, hash code =  -1973417279
'       n =   1101478715, hash code =   1101478715
'       n =   2078057429, hash code =   2078057429
'       n =   -334489950, hash code =   -334489950
'       n =    -68958230, hash code =    -68958230
'       n =   -379951485, hash code =   -379951485
'       n =    -31553685, hash code =    -31553685
'       n =   2105429592, hash code =   2105429592

通常情况下,类型具有多个数据字段可以参与生成的哈希代码。Frequently, a type has multiple data fields that can participate in generating the hash code. 若要生成的哈希代码的一种方法是组合使用这些字段XOR (eXclusive OR)操作,如下面的示例中所示。One way to generate a hash code is to combine these fields using an XOR (eXclusive OR) operation, as shown in the following example.

using System;

// A type that represents a 2-D point.
public struct Point
{
    private int x;
    private int y;

    public Point(int x, int y)
    {
       this.x = x;
       this.y = y;
    }
    
    public override bool Equals(Object obj)
    {
       if (! (obj is Point)) return false;
       
       Point p = (Point) obj;
       return x == p.x & y == p.y;
    }
    
    public override int GetHashCode()
    { 
        return x ^ y;
    } 
} 

public class Example
{
   public static void Main()
   {
      Point pt = new Point(5, 8);
      Console.WriteLine(pt.GetHashCode());
        
      pt = new Point(8, 5);
      Console.WriteLine(pt.GetHashCode());
   }
}
// The example displays the following output:
//       13
//       13
' A type that represents a 2-D point.
Public Structure Point
    Private x As Integer
    Private y As Integer

    Public Sub New(x As Integer, y As Integer)
       Me.x = x
       Me.y = y
    End Sub
    
    Public Overrides Function Equals(obj As Object) As Boolean
       If Not TypeOf obj Is Point Then Return False
       
       Dim p As Point = CType(obj, Point)
       Return x = p.x And y = p.y
    End Function
    
    Public Overrides Function GetHashCode() As Integer 
        Return x Xor y
    End Function 
End Structure 

Public Module Example
   Public Sub Main() 
      Dim pt As New Point(5, 8)
      Console.WriteLine(pt.GetHashCode())
        
      pt = New Point(8, 5)
      Console.WriteLine(pt.GetHashCode())
   End Sub 
End Module   

前面的示例返回相同的哈希代码 (n1,n2) 和 (n2,n1),因此可能会生成比需要的多个冲突。The previous example returns the same hash code for (n1, n2) and (n2, n1), and so may generate more collisions than are desirable. 提供多种解决方案,以便在这些情况下的哈希代码并不完全相同。A number of solutions are available so that hash codes in these cases are not identical. 一个是要返回的哈希代码Tuple反映每个字段的顺序的对象。One is to return the hash code of a Tuple object that reflects the order of each field. 下面的示例演示使用的可能实现Tuple<T1,T2>类。The following example shows a possible implementation that uses the Tuple<T1,T2> class. 但请注意,该实例化的性能开销Tuple对象可能会显著影响哈希表中存储大量对象的应用程序的整体性能。Note, though, that the performance overhead of instantiating a Tuple object may significantly impact the overall performance of an application that stores large numbers of objects in hash tables.

using System;

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

    public Point(int x, int y)
    {
       this.x = x;
       this.y = y;
    }
    
    public override bool Equals(Object obj)
    {
       if (!(obj is Point)) return false;
       
       Point p = (Point) obj;
       return x == p.x & y == p.y;
    }
    
    public override int GetHashCode()
    { 
        return Tuple.Create(x, y).GetHashCode();
    } 
} 

public class Example
{
   public static void Main()
   {
        Point pt = new Point(5, 8);
        Console.WriteLine(pt.GetHashCode());
        
        pt = new Point(8, 5);
        Console.WriteLine(pt.GetHashCode());
   }
}
// The example displays the following output:
//       173
//       269
Public Structure Point
    Private x As Integer
    Private y As Integer

    Public Sub New(x As Integer, y As Integer)
       Me.x = x
       Me.y = y
    End Sub
    
    Public Overrides Function Equals(obj As Object) As Boolean
       If Not TypeOf obj Is Point Then Return False
       
       Dim p As Point = CType(obj, Point)
       Return x = p.x And y = p.y
    End Function
    
    Public Overrides Function GetHashCode() As Integer 
        Return Tuple.Create(x, y).GetHashCode()
    End Function 
End Structure 

Public Module Example
    Public Sub Main() 
        Dim pt As New Point(5, 8)
        Console.WriteLine(pt.GetHashCode())
        
        pt = New Point(8, 5)
        Console.WriteLine(pt.GetHashCode())
    End Sub 
End Module         
' The example displays the following output:
'       173
'       269

第二种替代解决方案涉及的两个或多个位由连续的字段的哈希代码进行左移权重的单个哈希代码。A second alternative solution involves weighting the individual hash codes by left-shifting the hash codes of successive fields by two or more bits. 理想情况下,而不是被放弃,移出第 31 位应环绕在周围,而不被放弃。Optimally, instead of being discarded, bits shifted beyond bit 31 should wrap around rather than be discarded. 由于将丢弃由 C# 和 Visual Basic 中的左移运算符,这就需要创建一个左的 shift 包装方法如下所示:Since bits are discarded by the left-shift operators in both C# and Visual Basic, this requires creating a left shift-and-wrap method like the following:

public int ShiftAndWrap(int value, int positions)
{
    positions = positions & 0x1F;
  
    // Save the existing bit pattern, but interpret it as an unsigned integer.
    uint number = BitConverter.ToUInt32(BitConverter.GetBytes(value), 0);
    // Preserve the bits to be discarded.
    uint wrapped = number >> (32 - positions);
    // Shift and wrap the discarded bits.
    return BitConverter.ToInt32(BitConverter.GetBytes((number << positions) | wrapped), 0);
}
Public Function ShiftAndWrap(value As Integer, positions As Integer) As Integer
   positions = positions And &h1F
   
   ' Save the existing bit pattern, but interpret it as an unsigned integer.
   Dim number As UInteger = BitConverter.ToUInt32(BitConverter.GetBytes(value), 0)
   ' Preserve the bits to be discarded.
   Dim wrapped AS UInteger = number >> (32 - positions)
   ' Shift and wrap the discarded bits.
   Return BitConverter.ToInt32(BitConverter.GetBytes((number << positions) Or wrapped), 0)
End Function

下面的示例然后使用此 shift 包装方法来计算的哈希代码Point上述示例中使用的结构。The following example then uses this shift-and-wrap method to compute the hash code of the Point structure used in the previous examples.

using System;

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

    public Point(int x, int y)
    {
       this.x = x;
       this.y = y;
    }
    
    public override bool Equals(Object obj)
    {
       if (!(obj is Point)) return false;
       
       Point p = (Point) obj;
       return x == p.x & y == p.y;
    }
    
    public override int GetHashCode()
    { 
        return ShiftAndWrap(x.GetHashCode(), 2) ^ y.GetHashCode();
    } 
    
    private int ShiftAndWrap(int value, int positions)
    {
        positions = positions & 0x1F;
      
        // Save the existing bit pattern, but interpret it as an unsigned integer.
        uint number = BitConverter.ToUInt32(BitConverter.GetBytes(value), 0);
        // Preserve the bits to be discarded.
        uint wrapped = number >> (32 - positions);
        // Shift and wrap the discarded bits.
        return BitConverter.ToInt32(BitConverter.GetBytes((number << positions) | wrapped), 0);
    }
} 

public class Example
{
   public static void Main()
   {
        Point pt = new Point(5, 8);
        Console.WriteLine(pt.GetHashCode());
        
        pt = new Point(8, 5);
        Console.WriteLine(pt.GetHashCode());
   }
}
// The example displays the following output:
//       28
//       37 
Public Structure Point
    Private x As Integer
    Private y As Integer

    Public Sub New(x As Integer, y As Integer)
       Me.x = x
       Me.y = y
    End Sub
    
    Public Overrides Function Equals(obj As Object) As Boolean
       If Not TypeOf obj Is Point Then Return False
       
       Dim p As Point = CType(obj, Point)
       Return x = p.x And y = p.y
    End Function
    
    Public Overrides Function GetHashCode() As Integer 
        Return ShiftAndWrap(x.GetHashCode(), 2) XOr y.GetHashCode()
    End Function 
    
    Private Function ShiftAndWrap(value As Integer, positions As Integer) As Integer
        positions = positions And &h1F
      
        ' Save the existing bit pattern, but interpret it as an unsigned integer.
        Dim number As UInteger = BitConverter.ToUInt32(BitConverter.GetBytes(value), 0)
        ' Preserve the bits to be discarded.
        Dim wrapped AS UInteger = number >> (32 - positions)
        ' Shift and wrap the discarded bits.
        Return BitConverter.ToInt32(BitConverter.GetBytes((number << positions) Or wrapped), 0)
    End Function
End Structure 

Module Example
   Public Sub Main()
        Dim pt As New Point(5, 8)
        Console.WriteLine(pt.GetHashCode())
        
        pt = New Point(8, 5)
        Console.WriteLine(pt.GetHashCode())
   End Sub
End Module
' The example displays the following output:
'       28
'       37

Remarks

哈希代码是用于插入和标识基于哈希的集合中的对象,例如数值Dictionary<TKey,TValue>类,Hashtable类或从派生的类型DictionaryBase类。A hash code is a numeric value that is used to insert and identify an object in a hash-based collection such as the Dictionary<TKey,TValue> class, the Hashtable class, or a type derived from the DictionaryBase class. GetHashCode方法适用于需要快速检查的对象是否相等的算法提供此哈希代码。The GetHashCode method provides this hash code for algorithms that need quick checks of object equality.

备注

有关如何在哈希表中使用的哈希代码的信息以及某些其他哈希代码算法,请参阅哈希函数维基百科中的条目。For information about how hash codes are used in hash tables and for some additional hash code algorithms, see the Hash Function entry in Wikipedia.

是相等的相等返回哈希代码的两个对象。Two objects that are equal return hash codes that are equal. 但是,反过来则不适用: 相等哈希代码并不表示对象是否相等,因为不同的 (不相等) 对象可以具有相同哈希代码。However, the reverse is not true: equal hash codes do not imply object equality, because different (unequal) objects can have identical hash codes. 此外,.NET Framework 并不保证的默认实现GetHashCode方法,并且此方法所返回的.NET Framework 版本和平台,例如 32 位和 64 位平台之间可能不同的值。Furthermore, the .NET Framework does not guarantee the default implementation of the GetHashCode method, and the value this method returns may differ between .NET Framework versions and platforms, such as 32-bit and 64-bit platforms. 出于这些原因,请不要用于此方法的默认实现的唯一对象标识符作为哈希目的。For these reasons, do not use the default implementation of this method as a unique object identifier for hashing purposes. 从此请执行以下两种结果:Two consequences follow from this:

  • 不应假定相等哈希代码表示对象是否相等。You should not assume that equal hash codes imply object equality.

  • 应永远不会持久保存或在其中创建它,应用程序域之外使用的哈希代码,因为相同的对象可能哈希处理跨应用程序域、 进程和平台。You should never persist or use a hash code outside the application domain in which it was created, because the same object may hash across application domains, processes, and platforms.

警告

哈希代码旨在高效插入和基于哈希表的集合中查找。A hash code is intended for efficient insertion and lookup in collections that are based on a hash table. 哈希代码不是永久的值。A hash code is not a permanent value. 出于此原因:For this reason:

  • 请勿序列化哈希代码值或将其存储在数据库中。Do not serialize hash code values or store them in databases.
  • 不使用的哈希代码作为键来从键控集合中检索对象。Do not use the hash code as the key to retrieve an object from a keyed collection.
  • 不在应用程序域或进程之间发送的哈希代码。Do not send hash codes across application domains or processes. 在某些情况下,可能会在每个进程或每个应用程序域基础上计算的哈希代码。In some cases, hash codes may be computed on a per-process or per-application domain basis.
  • 不要使用而不是返回的加密哈希函数,如果需要加密型强哈希值的哈希代码。Do not use the hash code instead of a value returned by a cryptographic hashing function if you need a cryptographically strong hash. 对于加密哈希,请使用派生自的类System.Security.Cryptography.HashAlgorithmSystem.Security.Cryptography.KeyedHashAlgorithm类。For cryptographic hashes, use a class derived from the System.Security.Cryptography.HashAlgorithm or System.Security.Cryptography.KeyedHashAlgorithm class.
  • 测试的哈希代码,以确定两个对象相等。Do not test for equality of hash codes to determine whether two objects are equal. (不相等的对象可以有相同的哈希代码。)若要测试相等性,请调用ReferenceEqualsEquals方法。(Unequal objects can have identical hash codes.) To test for equality, call the ReferenceEquals or Equals method.

GetHashCode派生类型可以重写方法。The GetHashCode method can be overridden by a derived type. 如果GetHashCode是未重写,哈希代码为引用类型计算通过调用Object.GetHashCode方法的基类,计算哈希代码基于对象的引用; 有关详细信息,请参阅RuntimeHelpers.GetHashCodeIf GetHashCode is not overridden, hash codes for reference types are computed by calling the Object.GetHashCode method of the base class, which computes a hash code based on an object's reference; for more information, see RuntimeHelpers.GetHashCode. 换而言之,两个对象为其ReferenceEquals方法将返回true具有相同哈希代码。In other words, two objects for which the ReferenceEquals method returns true have identical hash codes. 如果值类型不会重写GetHashCode,则ValueType.GetHashCode基类的方法使用反射来计算基于类型的字段的值的哈希代码。If value types do not override GetHashCode, the ValueType.GetHashCode method of the base class uses reflection to compute the hash code based on the values of the type's fields. 换而言之,其字段具有相等的值的值类型具有相同哈希代码。In other words, value types whose fields have equal values have equal hash codes. 有关重写GetHashCode,请参阅"对继承者说明"部分。For more information about overriding GetHashCode, see the "Notes to Inheritors" section.

警告

如果重写GetHashCode方法,您还应该重写Equals,反之亦然。If you override the GetHashCode method, you should also override Equals, and vice versa. 如果被重写Equals方法将返回true两个对象是否相等,被重写的测试时GetHashCode方法必须返回两个对象相同的值。If your overridden Equals method returns true when two objects are tested for equality, your overridden GetHashCode method must return the same value for the two objects.

如果用作哈希表中的键的对象不提供的有用实现GetHashCode,可以通过提供指定哈希代码提供程序IEqualityComparer实现的重载之一Hashtable类构造函数。If an object that is used as a key in a hash table does not provide a useful implementation of GetHashCode, you can specify a hash code provider by supplying an IEqualityComparer implementation to one of the overloads of the Hashtable class constructor.

说明 Windows 运行时Windows RuntimeNotes for the Windows 运行时Windows Runtime

当您调用GetHashCode方法中的类Windows 运行时Windows Runtime,它不替代的类提供的默认行为GetHashCodeWhen you call the GetHashCode method on a class in the Windows 运行时Windows Runtime, it provides the default behavior for classes that don’t override GetHashCode. 这是.NET Framework 提供了有关支持的一部分Windows 运行时Windows Runtime(请参阅.NET Framework 支持的 Windows 应用商店应用程序和 Windows 运行时)。This is part of the support that the .NET Framework provides for the Windows 运行时Windows Runtime (see .NET Framework Support for Windows Store Apps and Windows Runtime). 中的类Windows 运行时Windows Runtime不继承Object,并不实现GetHashCodeClasses in the Windows 运行时Windows Runtime don’t inherit Object, and currently don’t implement a GetHashCode. 但是,它们显示为具有ToStringEquals(Object),和GetHashCode方法在 C# 或 Visual Basic 代码中,使用它们以及.NET Framework 提供的默认行为,这些方法时。However, they appear to have ToString, Equals(Object), and GetHashCode methods when you use them in your C# or Visual Basic code, and the .NET Framework provides the default behavior for these methods.

备注

Windows 运行时Windows Runtime 在 C# 或 Visual Basic 编写的类可以重写GetHashCode方法。 classes that are written in C# or Visual Basic can override the GetHashCode method.

Notes to Inheritors

哈希函数用于快速生成对象的值相对应的数字 (哈希代码)。 A hash function is used to quickly generate a number (hash code) that corresponds to the value of an object. 哈希函数通常特定于每种类型,和的唯一性,必须使用至少一个实例字段作为输入。 Hash functions are usually specific to each type and, for uniqueness, must use at least one of the instance fields as input. 不应使用静态字段的值来计算哈希代码。 Hash codes should not be computed by using the values of static fields. 类派生自Object,则GetHashCode方法可以委托给基类GetHashCode()实现仅当在派生的类定义为引用相等性的相等性。 For classes derived from Object, the GetHashCode method can delegate to the base class GetHashCode() implementation only if the derived class defines equality to be reference equality. 默认实现GetHashCode()对于引用类型返回的哈希代码,它等效于返回的GetHashCode(Object)方法。 The default implementation of GetHashCode() for reference types returns a hash code that is equivalent to the one returned by the GetHashCode(Object) method. 您可以重写GetHashCode()对于不可变的引用类型。 You can override GetHashCode() for immutable reference types. 一般情况下,对于可变引用类型,则应重写GetHashCode()仅当: In general, for mutable reference types, you should override GetHashCode() only if: -您可以计算从不可变; 的字段的哈希代码或 - You can compute the hash code from fields that are not mutable; or -你可以确保可变对象的哈希代码不会更改而该对象包含在一个集合,其中依赖于其哈希代码。 - You can ensure that the hash code of a mutable object does not change while the object is contained in a collection that relies on its hash code. 否则,您可能认为可变对象在哈希表中丢失。 Otherwise, you might think that the mutable object is lost in the hash table. 如果您选择要重写GetHashCode()可变引用类型,您的文档,应使其清除,而该对象存储在哈希表,您的类型的用户不应修改对象值。 If you do choose to override GetHashCode() for a mutable reference type, your documentation should make it clear that users of your type should not modify object values while the object is stored in a hash table. 对于值类型,GetHashCode()提供默认哈希代码实现使用反射。 For value types, GetHashCode() provides a default hash code implementation that uses reflection. 应考虑重写它的更好的性能。 You should consider overriding it for better performance.

有关详细信息和计算中有许多种哈希代码的示例,请参阅示例部分。 For more information and examples that compute hash codes in a variety of ways, see the Examples section.

哈希函数必须具有以下属性: A hash function must have the following properties: -如果两个对象的比较结果相等,GetHashCode()为每个对象的方法必须返回相同的值。 - If two objects compare as equal, the GetHashCode() method for each object must return the same value. 但是,如果两个对象不相等,比较GetHashCode()两个对象的方法不需要返回不同的值。 However, if two objects do not compare as equal, the GetHashCode() methods for the two objects do not have to return different values. -GetHashCode()对象的方法必须一致地返回相同的哈希代码,只要没有确定的对象的返回值的对象状态不修改System.Object.Equals方法。 - The GetHashCode() method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object's System.Object.Equals method. 请注意这是仅对应用程序,当前执行的则返回 true,如果再次运行该应用程序可以返回不同的哈希代码。 Note that this is true only for the current execution of an application, and that a different hash code can be returned if the application is run again. -对于获得最佳性能,哈希函数应生成均匀分布于所有输入,包括大量已群集化的输入。 - For the best performance, a hash function should generate an even distribution for all input, including input that is heavily clustered. 含意是对对象状态的小修改应导致大型修改哈希表的最佳性能的生成哈希代码。 An implication is that small modifications to object state should result in large modifications to the resulting hash code for best hash table performance. 哈希函数应为便宜的计算。 - Hash functions should be inexpensive to compute. -GetHashCode()方法不应引发异常。 - The GetHashCode() method should not throw exceptions. 例如,实现GetHashCode()方法提供的String类返回的相同的字符串值完全相同的哈希代码。 For example, the implementation of the GetHashCode() method provided by the String class returns identical hash codes for identical string values. 因此,两个String对象返回相同的哈希代码,如果它们表示相同的字符串值。 Therefore, two String objects return the same hash code if they represent the same string value. 此外,该方法使用的所有字符在字符串中生成合理地随机分布的输出,即使输入群集中的某些范围 (例如,许多用户可能只包含较低 128 ASCII 字符,即使的字符串字符串可以包含任何 65535 个 Unicode 字符)。 Also, the method uses all the characters in the string to generate reasonably randomly distributed output, even when the input is clustered in certain ranges (for example, many users might have strings that contain only the lower 128 ASCII characters, even though a string can contain any of the 65,535 Unicode characters). 在类上提供一个好的哈希函数会严重影响将这些对象添加到哈希表的性能。 Providing a good hash function on a class can significantly affect the performance of adding those objects to a hash table. 在使用提供的哈希函数的良好实现的密钥哈希表中,搜索的元素所用常量时间内的 (例如,o (1) 操作)。 In a hash table with keys that provide a good implementation of a hash function, searching for an element takes constant time (for example, an O(1) operation). 在与差劲的实现哈希函数的哈希表中,搜索的性能取决于哈希表中的项的数目 (例如,O (n) 操作,其中n是哈希表中的项数)。 In a hash table with a poor implementation of a hash function, the performance of a search depends on the number of items in the hash table (for example, an O(n) operation, where n is the number of items in the hash table). 恶意用户可以输入数据增多的冲突,这会显著降低取决于哈希表,在以下情况下的应用程序的性能: A malicious user can input data that increases the number of collisions, which can significantly degrade the performance of applications that depend on hash tables, under the following conditions: -当哈希函数生成频繁发生冲突。 - When hash functions produce frequent collisions. -当较大比例的哈希表中的对象生成的相等或大约等于另一个哈希代码。 - When a large proportion of objects in a hash table produce hash codes that are equal or approximately equal to one another. -当用户输入的数据从其计算哈希代码。 - When users input the data from which the hash code is computed. 派生类重写GetHashCode()还必须重写Equals(Object)以保证将被视为相等的两个对象具有相同的哈希代码; 否则为Hashtable类型可能无法正常工作。 Derived classes that override GetHashCode() must also override Equals(Object) to guarantee that two objects considered equal have the same hash code; otherwise, the Hashtable type might not work correctly.

Applies to

See Also