C# GetHashCode 问题
对于这种情况,重写 GetHashCode 函数的最佳方法是什么? 如果其中至少有一个字段匹配,我的对象将被视为相等。
在通用 Equals
方法的情况下,示例可能如下所示:
public bool Equals(Whatever other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
// Considering that the values can't be 'null' here.
return other.Id.Equals(Id) || Equals(other.Money, Money) ||
Equals(other.Code, Code);
}
不过,我对为这种情况制定良好的 GetHashCode
实现感到困惑。
这应该如何完成?
谢谢。
What would be the best way to override the GetHashCode
function for the case, when
my objects are considered equal if there is at least ONE field match in them.
In the case of generic Equals
method the example might look like this:
public bool Equals(Whatever other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
// Considering that the values can't be 'null' here.
return other.Id.Equals(Id) || Equals(other.Money, Money) ||
Equals(other.Code, Code);
}
Still, I'm confused about making a good GetHashCode
implementation for this case.
How should this be done?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是
Equals
的糟糕定义,因为它不具有传递性。考虑
然后
x == y
和y == z
但x != z
。此外,我们可以确定 GetHashCode 的唯一合理实现是常量映射。
假设 x 和 y 是不同的对象。设
z
为对象,然后
x == z
和y == z
,以便x.GetHashCode() == z。 GetHashCode()
和y.GetHashCode() == z.GetHashCode()
建立x.GetHashCode() == y.GetHashCode()
。由于x
和y
是任意的,我们已经确定GetHashCode
是常量。因此,我们已经表明
GetHashCode
的唯一可能实现是所有这些放在一起清楚地表明您需要重新思考您正在尝试模型的概念,并提出
的不同定义>等于
。This is a terrible definition of
Equals
because it is not transitive.Consider
Then
x == y
andy == z
butx != z
.Additionally, we can establish that the only reasonable implementation of
GetHashCode
is a constant map.Suppose that
x
andy
are distinct objects. Letz
be the objectThen
x == z
andy == z
so thatx.GetHashCode() == z.GetHashCode()
andy.GetHashCode() == z.GetHashCode()
establishing thatx.GetHashCode() == y.GetHashCode()
. Sincex
andy
were arbitrary we have established thatGetHashCode
is constant.Thus, we have shown that the only possible implementation of
GetHashCode
isAll of this put together makes it clear that you need to rethink the concept you are trying model, and come up with a different definition of
Equals
.我认为你不应该为此使用 Equals。人们对等于的含义有一个非常明确的概念,如果 ID 不同但代码或名称相同,我不会认为它们是“等于”。也许您需要一种不同的方法,例如“IsCompatible”。
如果您希望能够对它们进行分组,您可以在这些对象的列表上使用扩展方法 ToLookup(),以使用 IsCompatible 方法作为谓词。然后他们就会被分组。
I don't think you should be using Equals for this. People have a very explicit notion of what equals means, and if the Ids are different but the code or name are the same, I would not consider those "Equal". Maybe you need a different method like "IsCompatible".
If you want to be able to group them, you could use the extension method ToLookup() on a list of these objects, to use a predicate which would be your IsCompatible method. Then they would be grouped.
黄金法则是:如果对象比较相等,它们必须产生相同的哈希码。
因此,坦率地说
,如果 < code>Id、
Name
和Code
彼此独立,那么我不知道你是否可以做得更好。将这种类型的对象放入哈希表将会很痛苦。The golden rule is: if the objects compare equal, they must produce the same hash code.
Therefore a conforming (but let's say, undesirable) implementation would be
Frankly, if
Id
,Name
andCode
are independent of each other then I don't know if you can do any better. Putting objects of this type in a hash table is going to be painful.