JetBrains Rider 2024.1 Help

Code inspection: Overridden GetHashCode calls base 'Object.GetHashCode()'

In .NET, the GetHashCode() is used in built-in classes to efficiently look up objects in collections. When you override GetHashCode(), the aim is usually to provide a better hashing function that suits your specific object, mainly when its equality is not the same as the base Object equality.

While you can call base.GetHashCode() in your override, doing so is generally not useful because the base implementation does not know about your derived class's specific state and member variables. Thus, it might not provide a meaningful, well-distributed hash code for your custom object.

If you have overridden GetHashCode(), it is a signal that you are changing the equality semantics of the object, and so you should not then delegate back to the base class’s GetHashCode(). You should calculate the hash code based on your object's internal state instead.

Also, a very important rule in .NET is that if you override GetHashCode(), you also need to override Equals(), and they must be consistent. If Equals() says two objects are the same, then GetHashCode() must return the same hash code for them.

Last modified: 05 June 2024