【发布时间】:2009-03-12 14:08:06
【问题描述】:
我有一个包含以下两个属性的类:
public int Id { get; private set; }
public T[] Values { get; private set; }
我已经做到了IEquatable<T> 并像这样覆盖了object.Equals:
public override bool Equals(object obj)
{
return Equals(obj as SimpleTableRow<T>);
}
public bool Equals(SimpleTableRow<T> other)
{
// Check for null
if(ReferenceEquals(other, null))
return false;
// Check for same reference
if(ReferenceEquals(this, other))
return true;
// Check for same Id and same Values
return Id == other.Id && Values.SequenceEqual(other.Values);
}
当覆盖object.Equals 时,我当然也必须覆盖GetHashCode。但是我应该实现什么代码?如何从通用数组中创建哈希码?以及如何将它与Id 整数结合起来?
public override int GetHashCode()
{
return // What?
}
【问题讨论】:
标签: c# arrays generics hashcode