GetHashCode override of object containing generic array

前端 未结 9 1679
情歌与酒
情歌与酒 2020-12-04 05:35

I have a class that contains the following two properties:

public int Id      { get; private set; }
public T[] Values  { get; private set; }
<
9条回答
  •  遥遥无期
    2020-12-04 05:41

    Provided that Id and Values will never change, and Values is not null...

    public override int GetHashCode()
    {
      return Id ^ Values.GetHashCode();
    }
    

    Note that your class is not immutable, since anyone can modify the contents of Values because it is an array. Given that, I wouldn't try to generate a hashcode using its contents.

提交回复
热议问题