Share via


Object.GetHashCode Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: October 2010

Serves as a hash function for a particular type.

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

Syntax

'Declaration
Public Overridable Function GetHashCode As Integer
public virtual int GetHashCode()

Return Value

Type: System.Int32
A hash code for the current Object.

Remarks

A hash code is a numeric value that is used to identify an object during equality testing. It can also serve as an index for an object in a collection.

The GetHashCode method is suitable for use in hashing algorithms and data structures such as a hash table.

The default implementation of the GetHashCode method does not guarantee unique return values for different objects. Furthermore, the .NET Framework does not guarantee the default implementation of the GetHashCode method. Consequently, the default implementation of this method must not be used as a unique object identifier for hashing purposes.

The GetHashCode method can be overridden by a derived type. Value types must override this method to provide a hash function that is appropriate for that type and to provide a useful distribution in a hash table. For best results, the hash code must be based on the value of an instance field or property instead of a static field or property.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 GetHashCode returns equal values for different methods of the same class. GetHashCode returns zero for some user-defined structs in Silverlight for Windows Phone, but non-zero values in Silverlight.

Notes to Implementers

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 must use at least one of the instance fields as input.

A hash function must have the following properties:

  • If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values.

  • 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 Equals method. 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 must generate a random distribution for all input.

For example, the implementation of the GetHashCode method provided by the String class returns identical hash codes for identical string values. Therefore, two String objects return the same hash code if they represent the same string value. 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).

For derived classes of Object, the GetHashCode method can delegate to the Object.GetHashCode implementation, if and only if that derived class defines value equality to be reference equality and the type is not a value type.

Providing a good hash function on a class can significantly affect the performance of adding those objects to a hash table. In a hash table with a good implementation of a hash function, searching for an element takes constant time (for example, an O(1) operation). 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). Hash functions must also be inexpensive to compute.

Implementations of the GetHashCode method must not result in circular references. For example, if ClassA.GetHashCode calls ClassB.GetHashCode, ClassB.GetHashCode must not call ClassA.GetHashCode either directly or indirectly.

Implementations of the GetHashCode method must not throw exceptions.

Derived classes that override GetHashCode must also override Equals to guarantee that two objects considered equal have the same hash code.

Examples

In some cases, the GetHashCode method is implemented to simply return an integer value. The following code example illustrates an implementation of GetHashCode that returns an integer value.


Public Structure Int32
   Public value As Integer

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

using System;

public struct Int32
{
   public int value;

   //other methods...

   public override int GetHashCode()
   {
      return value;
   }
}

Frequently, a type has multiple data fields that can participate in generating the hash code. One way to generate a hash code is to combine these fields using an XOR (eXclusive OR) operation, as shown in the following code example.


Public Structure Point
   Public x As Integer
   Public y As Integer

   'other methods
   Public Overrides Function GetHashCode() As Integer
      Return x ^ y
   End Function 'GetHashCode
End Structure 'Point
using System;

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

   //other methods

   public override int GetHashCode()
   {
      return x ^ y;
   }
}

The following code example illustrates another case where the type's fields are combined using XOR (eXclusive OR) to generate the hash code. Notice that in this code example, the fields represent user-defined types, each of which implements GetHashCode and Equals.


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]
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();
   }
}

If the data member of the derived class is bigger than an Int32, you can combine the high order bits of the value with the low order bits using an XOR (eXclusive OR) operation, as shown in the following code example.


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
using System;

public struct Int64
{
   public long value;

   //other methods...

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

Version Information

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

Platforms

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

See Also

Reference

Change History

Date

History

Reason

October 2010

Added a definition of hash table.

Customer feedback.