Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to implement GetHashCode() for class with lots of properties?

Tags:

c#

hashcode

I have a class that has lots of properties that I am implementing IEquitable<T> on. I have found multiple examples on how to do GetHashCode() for small amount of properties.

Here is one example

public override int GetHashCode()
{
    unchecked // Overflow is fine, just wrap
    {
        int hash = 17;
        // Suitable nullity checks etc, of course :)
        hash = hash * 23 + field1.GetHashCode();
        hash = hash * 23 + field2.GetHashCode();
        hash = hash * 23 + field3.GetHashCode();
        return hash;
    }
}

How should I go around when I have hundreds of properties on object?

like image 993
Matas Vaitkevicius Avatar asked May 05 '14 09:05

Matas Vaitkevicius


People also ask

How is GetHashCode implemented?

For example, the implementation of the GetHashCode() method provided by the String class returns identical hash codes for identical string values. Therefore, two String objects return the same hash code if they represent the same string value.

Do I need to implement GetHashCode?

Yes, it is important if your item will be used as a key in a dictionary, or HashSet<T> , etc - since this is used (in the absence of a custom IEqualityComparer<T> ) to group items into buckets. If the hash-code for two items does not match, they may never be considered equal (Equals will simply never be called).

Why we use GetHashCode method 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.

Is GetHashCode unique C#?

NO! A hash code is not an id, and it doesn't return a unique value. This is kind of obvious, when you think about it: GetHashCode returns an Int32 , which has “only” about 4.2 billion possible values, and there's potentially an infinity of different objects, so some of them are bound to have the same hash code.


1 Answers

Spend the money to get a tool like Resharper, then just do Alt+Ins then E. This will bring up the "Generate Equality Members" dialog

enter image description here

From there just check the 100 boxes you need and it will autogenerate the GetHashCode() and Equals() functions for you

enter image description here
(the above took about 10 seconds to create)

Resharper does so much more too that it makes it worth the $150 for a personal license (you can use a personal license for work related activities without violating it, I checked). And if you are not making enough money as a programmer to afford a one time investment of $150 you really should start looking elsewhere to work as you are being very underpaid. (If you don't make any money as a programmer as you are working on a open source project Resharper is free for development teams of open source projects)

like image 160
Scott Chamberlain Avatar answered Nov 04 '22 01:11

Scott Chamberlain