【发布时间】:2013-10-08 23:22:12
【问题描述】:
在这篇 MSDN 文章中 http://msdn.microsoft.com/en-us/library/ms132123.aspx 它讨论了 Class Equalitycomparer 并有一个例子。在这个关于比较盒子的例子中,它有这个类 -
class BoxSameDimensions : EqualityComparer<Box>
{
public override bool Equals(Box b1, Box b2)
{
if (b1.Height == b2.Height & b1.Length == b2.Length
& b1.Width == b2.Width)
{
return true;
}
else
{
return false;
}
}
public override int GetHashCode(Box bx)
{
int hCode = bx.Height ^ bx.Length ^ bx.Width;
return hCode.GetHashCode();
}
}
我不明白这行 int hCode = bx.Height ^ bx.Length ^ bx.Width;
有人可以解释一下吗?为什么是异或?
【问题讨论】:
-
另见stackoverflow.com/a/2334251/945456和csharpindepth.com/ViewNote.aspx?NoteID=27(后者解释了为什么异或实际上可能是一个坏主意)