When String.GetHashCode() in .NET C# Drives You Crazy
top of page

Subscribe to get best practices, tutorials, and many other cool things directly to your email inbox

  • Writer's pictureAhmed Tarek

When String.GetHashCode() in .NET C# Drives You Crazy

Updated: Apr 17

Know when to depend on String.GetHashCode() in .NET C#, and when not.


Know when to depend on String.GetHashCode() in DotNet (.NET) CSharp (C#), and when not. Best Practice Code Coding Programming Software Development Architecture Engineering
Photo by Bozhin Karaivanov on Unsplash

The Story


I was working on a side project of a tool to help me with my daily work. In one module, I needed to keep track of some transactions which could be executed between the tool runs. In other words, when I close and open the tool, these transactions should be there.


For storage, I used a SQLite database where I save a hash-code of an entity representing my Transaction class. I was depending on this hash-code as at some point the tool would generate a new hash-code and compare it to the one already saved in the database.


So, after setting everything in place, I started the tool, give it a try, yes… it is working. I close the tool, do some stuff, then try testing another thing, now it is not working as it should!!


I kept trying to understand what is going on and finally I got it. The hash-code comparison is not working fine. During the same run session of the tool, the comparison is working fine. However, when I close and open the tool, the newly generated hash-code -to the same exact transaction-is not the same as the one saved in the database.


I searched the internet and found that this is true. According to Microsoft:


The hash code itself is not guaranteed to be stable. Hash codes for identical strings can differ across .NET implementations, across .NET versions, and across .NET platforms (such as 32-bit and 64-bit) for a single version of .NET. In some cases, they can even differ by application domain. This implies that two subsequent runs of the same program may return different hash codes.
As a result, hash codes should never be used outside of the application domain in which they were created, they should never be used as key fields in a collection, and they should never be persisted.
Finally, don’t use the hash code instead of a value returned by a cryptographic hashing function if you need a cryptographically strong hash. For cryptographic hashes, use a class derived from the System.Security.Cryptography.HashAlgorithm or System.Security.Cryptography.KeyedHashAlgorithm class.
For more information about hash codes, see Object.GetHashCode.

Therefore, I am now sharing this with you and I am going to tell you how to overcome this in a good way.


 


 

Know when to depend on String.GetHashCode() in DotNet (.NET) CSharp (C#), and when not. Best Practice Code Coding Programming Software Development Architecture Engineering
Photo by Brett Jordan on Unsplash

Let’s Give It a Try


Create a Console Application with the following code.



A simple class Employee with a simple implementation of IEquatable<Employee> interface.


Just notice how the GetHashCode method is implemented.


When running the application, this is what we get:


Know when to depend on String.GetHashCode() in DotNet (.NET) CSharp (C#), and when not. Best Practice Code Coding Programming Software Development Architecture Engineering

Now, stop the application, and run it again. This is what we get:


Know when to depend on String.GetHashCode() in DotNet (.NET) CSharp (C#), and when not. Best Practice Code Coding Programming Software Development Architecture Engineering

See, both results are not the same.


 


 

Know when to depend on String.GetHashCode() in DotNet (.NET) CSharp (C#), and when not. Best Practice Code Coding Programming Software Development Architecture Engineering
Photo by Volkan Olmez on Unsplash

The Right Way to Fix This



ArrayEqualityComparer<T>





StringExtensions




FixedEmployee




When running the application, this is what we get:


Know when to depend on String.GetHashCode() in DotNet (.NET) CSharp (C#), and when not. Best Practice Code Coding Programming Software Development Architecture Engineering

Now, stop the application, and run it again. This is what we get:


Know when to depend on String.GetHashCode() in DotNet (.NET) CSharp (C#), and when not. Best Practice Code Coding Programming Software Development Architecture Engineering

See, both results are the same.


Know when to depend on String.GetHashCode() in DotNet (.NET) CSharp (C#), and when not. Best Practice Code Coding Programming Software Development Architecture Engineering
Photo by MIO ITO on Unsplash

Summary


If you need the result of String.GetHashCode() to be persistent between your application run sessions, you will need to follow this way or another similar way. Otherwise, you will end up with a new result every time you stop and run the application.


That’s it, hope you found reading this story as interesting as I found writing it.



Recent Posts

See All

Subscribe to get best practices, tutorials, and many other cool things directly to your email inbox

bottom of page
Mastodon Mastodon