An object in C# has four methods - {Equals, GetType, ToString, GetHashCode}.
What sort of useful thing could someone do with the hash-code?
4 Answers
What sort of useful thing could someone do with the hashcode?
Quickly find potentially equal objects.
In particular, this method is usually used by types such as Dictionary<TKey, TValue> (for the keys) and HashSet<T>.
You should not assume that objects with equal hash codes are equal, however. See Eric Lippert's blog post for more information, and the Wikipedia hash table page for a more general discussion on the uses of hash codes.
7 Comments
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, and the value it returns will be the same between different versions of the .NET Framework. 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 uniqueness, the hash code must be based on the value of an instance field or property instead of a static field or property.
Objects used as a key in a Hashtable object must also override the GetHashCode method because those objects must generate their own hash code. If an object used as a key does not provide a useful implementation of GetHashCode, you can specify a hash code provider when the Hashtable object 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.
Comments
The basic idea is that if two objects have a different hash code, they are different. If they have the same hash code, they could be different or equal.
To check if an object is present in a collection, you can first check against the hash codes, which is quick given you are comparing integers, and then do a more accurate test only on the objects with the same hash code.
This is used in the collection classes, for example.
Comments
GetHashCode
GetHashCode exists only for the benefit of these two types
->HashTable
->GenericDictionary
GetHashCode gives you varied keys for good hashtable performance.
Equals
Equals provides a null-safe equality comparison when types are unknown at compile time. its signature is
public static bool Equals(object A,object B).
So you cant use operators like == or != if the type is unknown at compile time.You have to use Equals
Its useful when writing generic types
For 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 default texual representation of a type instance.This method is overridden by all built in types
GetType
GetType is evaluated at runtime.It helps us to know the type's name,assemby,base type..and others