Item 7: Understand the Pitfalls of GetHashCode()(Effective C#)
转载 Summarizing the default behavior, Object.GetHashCode() works correctly for reference types, although it does not necessarily generate an efficient distribution. (If you have overridden Object.operator==(), you can break GetHashCode()). ValueType.GetHashCode() works only if the first field in your struct is read-only. ValueType.GetHashCode() generates an efficient hash code only when the first field in your struct contains values across a meaningful subset of its inputs.
GetHashCode() has very specific requirements: Equal objects must produce equal hash codes, and hash codes must be object invariants and must produce an even distribution to be efficient. All three can be satisfied only for immutable types.
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:Item 8: Prefer Query Syntax to Loops(Effective C#)
下一篇:Item 6: Understand the Relationships Among the Many Different Concepts of Equality(Effective C#)

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C# 语法中级
C# 语法中级
c# lambda