asp.net.ph

Object.GetHashCode Method

System Namespace   Object Class


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

[ VB ]
<Serializable>
<ClassInterface ( ClassInterfaceType.AutoDual ) >
Overridable Public Function GetHashCode ( ) As Integer

[ C# ]
[ Serializable ]
[ ClassInterface ( ClassInterfaceType.AutoDual ) ]
public virtual int GetHashCode ( );

[ C++ ]
[ Serializable ]
[ ClassInterface ( ClassInterfaceType.AutoDual ) ]
public: virtual int GetHashCode ( );

[ JScript ]
public Serializable
   ClassInterface ( ClassInterfaceType.AutoDual )
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.

This implementation of Object.GetHashCode can only guarantee that the same hash code will be returned for the same instance; it cannot guarantee that different instances will have different hash codes or that two objects referring to the same value will have the same hash codes. Different versions of the .NET Framework might also generate different hash codes for the same instance. Therefore, do not persist hash codes to files or send them over the network. To guarantee the same hash code for the same object, you must define your own immutable hash function using the System.Collections.IHashCodeProvider interface and use it consistently.

The default implementation returns an index for the object determined by the common language runtime. The index is unique to an instance of an object within an AppDomain for an instance of the executing engine. However, because this index can be reused after the object is reclaimed during garbage collection, it is possible to obtain the same hash code for two different objects. Also, two objects that represent the same value have the same hash code only if they are the exact same object. This implementation is not particularly useful for hashing; therefore, derived classes should override GetHashCode.

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 should 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 should generate a random distribution for all input.
  • A hash function should be based on an immutable data member. The hash function should return exactly the same value regardless of any changes that are made to the object. Basing the hash function on a mutable data member can cause serious problems, including never being able to access that object in a hash table.

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. One way to ensure this is by basing the hash code on an immutable data member. 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 should 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#, 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#, 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#, 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 ( );
   }
}

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

[ 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;
   }
}
See Also

Object Members Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph