【发布时间】:2021-05-30 09:18:31
【问题描述】:
我正在写这门课:
private class SameCellsComparer : EqualityComparer<Links>
{
public override bool Equals(Links t1, Links t2)
{
return t1 == null || t1.Equals(t2);
}
public override int GetHashCode(Links t)
{
// we suppose there will always be less than 100 000 000 items:
return t.Item1.id + t.Item2.id * 100000000;
}
}
但是 Rider 建议我在 GetHashCode(Links t) 上使用解构,如果我应用他的建议,我会得到:
private class SameCellsComparer : EqualityComparer<Links>
{
public override bool Equals(Links t1, Links t2)
{
return t1 == null || t1.Equals(t2);
}
public override int GetHashCode(Links t)
{
// we suppose there will always be less than 100 000 000 items:
(Cell item1, Cell item2) = t;
return item1.id + item2.id * 100000000;
}
}
请不要说做坏事GetHashCode()原则,我只求转换成(Cell item1, Cell item2) = t:是不是更安全、更快、更干净?没看懂。
【问题讨论】:
-
如果你看list of all inspections这个默认的严重级别是“提示”——最低级别是described as“这个严重级别的代码问题会引起你对特定代码细节的注意和/或建议改进方法”。