C# - Generic HashCode implementation for classes

后端 未结 3 1337
独厮守ぢ
独厮守ぢ 2021-01-02 05:17

I\'m looking at how build the best HashCode for a class and I see some algorithms. I saw this one : Hash Code implementation, seems to be that .NET classes HashCode methods

3条回答
  •  时光取名叫无心
    2021-01-02 05:43

    Instead of calling keys[i].GetType().IsArray, you should try to cast it to IEnumerable (using the as keyword).

    You can fix the Equals method without repeating the field list by registering a static list of fields, like I do here using a collection of delegates.
    This also avoids the array allocation per-call.

    Note, however, that my code doesn't handle collection properties.

提交回复
热议问题