Share via


ModuloObjectHashcode 

The ModuloObjectHashcode managed debugging assistant (MDA) changes the behavior of the Object class to perform a modulo operation on the hash code returned by the GetHashCode method. The default modulus for this MDA is 1, which causes GetHashCode to return 0 for all objects.

Symptoms

After moving to a new version of the common language runtime (CLR), a program no longer executes properly:

  • The program is getting the wrong object from a Hashtable.

  • The order of enumeration from a Hashtable has a change that breaks the program.

  • Two objects that used to be equal are no longer equal.

  • Two objects that used to not be equal are now equal.

Cause

Your program may be getting the wrong object from a Hashtable because the implementation of the Equals method on the class for the key into the Hashtable tests for equality of objects by comparing the results of the call to the GetHashCode method. Hash codes should not be used to test for object equality because two objects may have the same hash code even if their respective fields have different values. These hash code collisions, although rare in practice, do occur. The effect this has on a Hashtable lookup is that two keys which are not equal appear to be equal, and the wrong object is returned from the Hashtable. For performance reasons, the implementation of GetHashCode can change between runtime versions, so collisions that might not occur on one version might occur on subsequent versions. Enable this MDA to test whether your code has bugs when hash codes collide. When enabled, this MDA causes the GetHashCode method to return 0, resulting in all hash codes colliding. The only effect enabling this MDA should have on your program is that your program runs slower.

The order of enumeration from a Hashtable may change from one version of the runtime to another if the algorithm used to compute the hash codes for the key change. To test whether your program has taken a dependency on the order of enumeration of keys or values out of a hash table, you can enable this MDA.

Resolution

Never use hash codes as a substitute for object identity. Implement the override of the System.Object.Equals(System.Object) method to not compare hash codes.

Do not create dependencies on the order of enumerations of keys or values in hash tables.

Effect on the Runtime

Applications run more slowly when this MDA is enabled. This MDA simply takes the hash code that would have been returned and instead returns the remainder when divided by a modulus.

Output

There is no output for this MDA.

Configuration

The modulus attribute specifies the modulus used on the hash code. The default value is 1.

<mdaConfig>
  <assistants>
    <moduloObjectHashcode modulus="1" />
  </assistants>
</mdaConfig>

See Also

Reference

System.Object.GetHashCode
System.Object.Equals(System.Object)

Concepts

Diagnosing Errors with Managed Debugging Assistants