Simplify Overriding Equals(), GetHashCode() in C# for Better Maintainability

前端 未结 3 1844
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-11 03:20

I find my self overriding Equals() and GetHashCode() frequently to implement the semantic that business objects with identical property values are

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-11 04:14

    Perhaps I'm confused here, but shouldn't the on null check return a 1 instead of a 0 in the GetHashCode override?

    So

    MyStringProp == null ? 0 : MyStringProp.GetHashValue()
    

    should be

    MyStringProp == null ? 1 : MyStringProp.GetHashValue()
    

提交回复
热议问题