Home » Advanced CSharp » What is .NET’s Object.GetHashCode() Used For?

What is .NET’s Object.GetHashCode() Used For?


Here is a great question from a visitor.

“What is the exact use of GetHashCode of an object in .net? Does it have any relation with garbage collection?”

Let’s answer the second question first. No, it has nothing to do with garbage collection.

According to the Microsoft documentation, “The GetHashCode method is suitable for use in hashing algorithms and data structures such as a hash table.”

I suppose that sentence isn’t very helpful if you don’t know what a hashing algorithm is or why you would use one.  So first let’s back up and examine those topics.

Hashing algorithms take a very large amount of data and reduce it to a small “number” that can be used to represent that data.  While the result is not THE data, it is statistically unique enough that we can use the number in place of the data for specific purposes.

One place where hashing is used consistently is in storing passwords.  This is not done all of the time, but in most secure systems, this is how the password is stored.  We compute the hash and store the hash of the password.  When the user logs in, we hash the password they give us and compare the hash we computed to the hash that is stored in the database.

Another place hashing is used is when we want to create a list of items in a collection that can be retrieved quickly without having to create a huge array to hold each possible item that might be added.

For example, we want to create a list of items that only has unique items in it.  For this we need to create a hashing algorithm that gives us statistically unique “numbers” for any element it is given.  Notice I say statistically unique.  This is because going from a large value to a smaller value will always mean there is a possibility that two hash values will be the same for two sets of data that are different.  That’s the nature of hashing.

But, assuming that all of our hash values ARE unique, we can compute the hash of the original object and check the location where that object would exist in our array and see if there is something already there.

This is, in fact, how most HashSet classes are implemented.

One way of producing the hash value is to line up all of the bytes or characters that make up the object to be hashed and assign them to the number, in this case an integer in a loop, bit shifting them prior to adding any new value.

public int GetHashCode()
    int hash = 0;
    char[] objectList = this.ToCharArray();
    foreach (char c in objectList)
        hash = hash << 1;
        hash += c;
    return hash;

One thing I mentioned above probably needs a bit of clarifying.  I’ve placed the word “number” in quotes for much of this discussion because when we hash we really aren’t too concerned with the fact that the item we are hashing actually ends up being a number.  For the GetHashCode() method, yes, it is important.  But if you wanted to hash a password, you’d want a much larger number than an integer to reduce the chance that any two passwords created the same hash.  In that case, you’d probably store the hash in a character array, which you could store into a string in your database.


Other post in Advanced CSharp

Related Post

  • CSharp VAR MisconceptionsCSharp VAR Misconceptions I find it amazing that .NET 3.5 has been out for over a year and people still don’t understand the “var” keyword. Just last week I got a comment on one of my videos asking why I used […]
  • .Net String Pool – Not Just For The Compiler.Net String Pool – Not Just For The Compiler On Monday, I was corrected in my assertion that creating multiple empty strings would create multiple objects.  Turns out the compiler automatically puts all of the strings that are […]
  • Forcing Extension Methods to Be UsedForcing Extension Methods to Be Used I received the following question: Is it possible to force a namespace to be included so that the extension methods in it will be used in it instead of the methods in the main class? I […]
  • Storing An Image To a Database in .NETStoring An Image To a Database in .NET Several weeks ago I mentioned that I store the images that the user uploads to the system into the database. Some of you have expressed an interest in how I do that.  So I plan to […]
  • Reflection – Accessing MethodsReflection – Accessing Methods Today is the next to last installment on reflection.  We will look at how to call methods and pass parameters.  This is going to look a lot like what we've already done in […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

One Pingback/Trackback