Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would I do with a .NET object hashcode?

Tags:

c#

.net

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

like image 786
patrick Avatar asked Oct 31 '12 14:10

patrick


People also ask

What is the use of hashCode in C#?

A hash code is a numeric value which is used to insert and identify an object in a hash-based collection. The GetHashCode method provides this hash code for algorithms that need quick checks of object equality. Syntax: public virtual int GetHashCode ();

What is meant by hashCode?

A hashcode is an integer value associated with every object in Java, facilitating the hashing in hash tables. To get this hashcode value for an object, we can use the hashcode() method in Java. It is the means hashcode() method that returns the integer hashcode value of the given object.

Why do we need to override hashCode and equals method C#?

If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.

What does hashCode () method of the object class return?

hashCode() is the method of Object class. This method returns a hash code value for the object.


1 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.

like image 56
Jon Skeet Avatar answered Oct 02 '22 20:10

Jon Skeet