我有一个类,其中有很多我正在实现的属性IEquitable<T>
在。我找到了多个关于如何对少量属性执行 GetHashCode() 的示例。
这是一个例子 https://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
int hash = 17;
// Suitable nullity checks etc, of course :)
hash = hash * 23 + field1.GetHashCode();
hash = hash * 23 + field2.GetHashCode();
hash = hash * 23 + field3.GetHashCode();
return hash;
}
}
当对象有数百个属性时,我应该如何处理?
Spend the money to get a tool like Resharper http://www.jetbrains.com/resharper/, then just do Alt+Ins then E. This will bring up the "Generate Equality Members" dialog
从那里只需选中您需要的 100 个框,它就会自动生成GetHashCode()
and Equals()
为您提供的功能
(the above took about 10 seconds to create)
Resharper 还可以做更多的事情,因此值得花费 150 美元购买个人许可证(我检查过,您可以使用个人许可证进行与工作相关的活动而不违反它)。如果你作为一名程序员赚的钱不足以承担 150 美元的一次性投资,你真的应该开始寻找其他地方的工作,因为你的工资非常低。 (如果你作为一名程序员没有赚到任何钱,因为你正在开发一个开源项目 Resharper免费供开源项目的开发团队使用 http://www.jetbrains.com/resharper/buy/choose_edition.jsp?license=OPEN_SOURCE)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)