General advice and guidelines on how to properly override object.GetHashCode()

前端 未结 11 1660
终归单人心
终归单人心 2020-12-01 00:09

According to MSDN, a hash function must have the following properties:

  1. If two objects compare as equal, the GetHashCode method for each object m

11条回答
  •  [愿得一人]
    2020-12-01 00:31

    public override int GetHashCode()
    {
        return IntProp1 ^ IntProp2 ^ StrProp3.GetHashCode() ^ StrProp4.GetHashCode ^ CustomClassProp.GetHashCode;
    }
    

    Do the same in the customClass's GetHasCode method. Works like a charm.

提交回复
热议问题