Export (0) Print
Expand All

Object.GetHashCode Method

Serves as a hash function for a particular type, suitable for use in hashing algorithms and data structures like a hash table.

[Visual Basic]
Public Overridable Function GetHashCode() As Integer
[C#]
public virtual int GetHashCode();
[C++]
public: virtual int GetHashCode();
[JScript]
public function GetHashCode() : int;

Return Value

A hash code for the current Object.

Remarks

This method can be overridden by a derived class. Value classes must override this method to provide a hash function that is appropriate for the class and that ensures a better distribution in the hash table. Classes that might be used as a key in a hash table must also override this method, because objects that are used as keys in a hash table are required to generate their own hash code through this method. However, if the objects that are used as keys do not provide a useful implementation of GetHashCode, you can provide a different hash code provider, that is based on the System.Collections.IHashCodeProvider interface, when the Hashtable is constructed.

The default implementation of GetHashCode does not guarantee uniqueness or consistency; therefore, it must not be used as a unique object identifier for hashing purposes. Derived classes must override GetHashCode with an implementation that returns a unique hash code. 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.

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 of the same type represent the same value, the hash function must return the same constant value for either object.
  • For the best performance, a hash function must generate a random distribution for all input.
  • The hash function must return exactly the same value regardless of any changes that are made to the object.

For example, the implementation of GetHashCode provided by the String class returns unique hash codes for unique 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).

GetHashCode must always return the same value for a given instance of the object. For derived classes of Object, GetHashCode 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 GetHashCode 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 GetHashCode 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; otherwise, Hashtable might not work correctly.

Example

[C#, C++, JScript] In some cases, GetHashCode is implemented to simply return an integer value. The following code example illustrates an implementation of GetHashCode, which returns an integer value.

[C#] 
using System;

public struct Int32 {
   public int value;

   //other methods...

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

[C++] 
#using <mscorlib.dll>
using namespace System;

public __value struct Int32 {
public:
   int value;

   //other methods...

   int GetHashCode() {
      return value;
   }
};

[C#, C++, JScript] 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.

[C#] 
using System;

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

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

[C++] 
#using <mscorlib.dll>
using namespace System;

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

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

[C#, C++, JScript] 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.

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

[C++] 
#using <mscorlib.dll>
using namespace System;

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

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

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

public __gc class MyClass {
   SomeType* a;
   AnotherType* b;
   LastType* c;

public:
   int GetHashCode () {
     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();
   }
}

[C#, C++, JScript] 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.

[C#] 
using System;

public struct Int64 {
   public long value;

   //other methods...

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

[C++] 
#using <mscorlib.dll>
using namespace System;

public __value struct Int64 {
public:
   long value;

   //other methods...

   int GetHashCode() {
      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;
   }
}

[Visual Basic] No example is available for Visual Basic. To view a C#, C++, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Object Class | Object Members | System Namespace | Hashtable Collection Type | Hashtable

Show:
© 2014 Microsoft