What should I do with the .NET hash code?

An object in C # has four methods - { Equals , GetType , ToString , GetHashCode }.
What useful thing can someone do with a hash code?

+6
c #
source share
4 answers

What useful thing can a person with a hash code do?

Quickly find potentially equal objects.

In particular, this method is usually used by the types Dictionary<TKey, TValue> (for keys) and HashSet<T> .

You should not assume that objects with equal hash codes are equal. See Eric Lippert's blog post for more information, and the Wikipedia hash table for a more general discussion on the use of hash codes.

+16
source share

A hash code is a numeric value that is used to identify an object during equality checking. 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 is not guaranteed to guarantee unique return values ​​for different objects. In addition, the .NET Framework does not guarantee a standard implementation of the GetHashCode method and the return value will be the same between different versions of the .NET Framework. Therefore, the default implementation of this method should not be used as a unique identifier for hashing purposes.

The GetHashCode method can be overridden by a derived type. Cost types must redefine this method to provide a hash function that is suitable for that type and provides useful distribution to the hash table. For uniqueness, the hash code should be based on the value of a field or instance property instead of a static field or property.

The objects used as the key in the Hashtable object must also override GetHashCode, because these objects must generate their own hash code. If the object used as the key does not provide a useful implementation of GetHashCode, you can specify the hash provider when the Hashtable is constructed. Prior to the .NET Framework version 2.0, the hash code provider was based on the System.Collections.IHashCodeProvider Interface. Starting with version 2.0, the hash code provider is based on the System.Collections.IEqualityComparer Interface.

- Received from MSDN :

+2
source share

The basic idea is that if two objects have a different hash code, they are different. If they have the same hash code, they can be different or equal.

To check if an object is present in the collection, you can first check the hash codes, which is quick if you compare integers and then perform a more accurate test only on objects with the same hash code.

This is used, for example, in collection classes.

0
source share

Gethashcode

GetHashCode exists only in the interests of these two types.

-> HashTable

-> GenericDictionary

GetHashCode provides you with a variety of keys for good hashtable performance.

Equally

Equals provides a null-safe equality comparison when types are unknown at compile time. his signature

public static bool Equals(object A,object B) .

Thus, you cannot use operators such as == or != If the type is unknown at compile time. You have to use Equals

This is useful when writing generic types.

Example:

 class Test<T> { T value; public void SetV(T newValue) { if(object.Equals(newValue,value)) //We have to use Object.Equals cant use == or !=since they cannot bind to unknown type at compile time } } 

Tostring

It returns the default texual representation type instance. This method is overridden by all built-in types.

Gettype

GetType is evaluated at runtime. It helps us find out the type name , assemby , base type .. and others

0
source share

All Articles