[英]Overriding Equals and GetHashCode - default implementation in derived class
[英]GetHashCode implementation using tuples for derived class
我喜歡使用元組來實現 GetHashCode,如此處所述https://intellitect.com/overidingobjectusingtuple/ 。 但是,這似乎不適用於派生的 class。例如:
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。 有什么建議么? 我們目前正在這樣做,但我很想擺脫未檢查和 hash 常量:
public override int GetHashCode()
{
unchecked
{
return (base.GetHashCode() * HashCodeConstant) ^ (Prop3, Prop4).GetHashCode();
}
}
請參閱https://learn.microsoft.com/en-us/do.net/fundamentals/code-analysis/style-rules/ide0070
public override int GetHashCode()
{
return System.HashCode.Combine(base.GetHashCode(), Prop3, Prop4);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.