【发布时间】:2021-10-05 20:00:00
【问题描述】:
我喜欢使用元组来实现 GetHashCode,如 https://intellitect.com/overidingobjectusingtuple/ 所述。但是,这似乎不适用于派生类。例如:
class Base
{
public int Prop1 { get; set; }
public int Prop2 { get; set; }
public override int GetHashCode() => (Prop1, Prop2).GetHashCode();
}
class Derived : Base
{
public int Prop3 { get; set; }
public int Prop4 { get; set; }
public override int GetHashCode() => (Prop3, Prop4, ???).GetHashCode();
}
目前还不清楚我将如何使用元组实现 Derived GetHashCode。有什么建议么?我们目前正在这样做,但我很想摆脱未经检查的哈希常量:
public override int GetHashCode()
{
unchecked
{
return (base.GetHashCode() * HashCodeConstant) ^ (Prop3, Prop4).GetHashCode();
}
}
【问题讨论】:
标签: c# tuples gethashcode