hashCode() method when equals() is based on multiple independent fields

前端 未结 10 2146
执念已碎
执念已碎 2021-02-08 09:16

i have a class whose equality is based on 2 fields such that if either one is equal then the objects of this type are considered equal. how can i write a hashCode() function for

10条回答
  •  既然无缘
    2021-02-08 09:41

    How about

    public override int GetHashCode()
    {
        return id.GetHashCode() ^ name.GetHashCode();
    }
    

提交回复
热议问题