C# Equals 扩展无法检查是否相等
C# Equals extension is unable to check equality
我扩展了 equals 方法和哈希码来检查两个具有布尔属性的相同对象是否相等。当我改变对象使布尔属性之一为 false 而不是 true 时,它无法识别差异并断言它们相等;。有什么想法吗?
public override bool Equals(object value)
{
if (!(value is LocationPropertyOptions options))
return false;
return Equals(options, value);
}
public bool Equals(LocationPropertyOptions options)
{
return options.GetHashCode() == GetHashCode();
}
public override int GetHashCode()
{
return ToString().GetHashCode();
}
public override string ToString()
{
return $"{Identifier}{AccountEntityKey}{Address}{Comments}{Contact}" +
$"{Coordinate}{Description}{FaxNumber}{LastOrderDate}{PhoneNumber}" +
$"{ServiceAreaOverride}{ServiceRadiusOverride}{StandardInstructions}" +
$"{WorldTimeZone_TimeZone}{ZoneField}{CommentsOptions}";
}
您从 value
转换 options
,然后用 options
与 value
调用 Equals
。这意味着您将 value
与 value
进行比较,它 returns 总是 true
适合您
public override bool Equals(object value)
{
if (!(value is LocationPropertyOptions options))
return false;
return Equals(options, value);
}
尝试比较 this
和 value
,例如
return Equals(this, value);
这并没有真正回答你的问题。但是,在实现相等性以避免此类错误时,您应该考虑一些事项。
首先你有两个完全不同的实现来表示相等。您的 override bool Equals(object value)
重定向到静态方法 Object.Equals(object, object)
,它只执行 ReferenceEquals
。另一方面,您的 public bool Equals(LocationPropertyOptions)
(可能是 IEquatable<LocationPropertyOptions>
的实现)只是使用了您奇怪的 GetHashCode
实现,即
第二点:您不应在哈希码实现中使用可变成员,尤其是当您的对象存储在字典或哈希图中时,这在很大程度上取决于哈希码的良好实现。参见 MSDN on GetHashCode
You can override GetHashCode() for immutable reference types. In
general, for mutable reference types, you should override
GetHashCode() only if:
You can compute the hash code from fields that are not mutable; or
You can ensure that the hash code of a mutable object does not change
while the object is contained in a collection that relies on its hash
code.
第三个也是最后一个:您不应该在检查相等性时使用 GetHashCode
:
Do not test for equality of hash codes to determine whether two
objects are equal
虽然假定相同的对象具有相同的哈希码,但不同的对象可能无论如何都具有完全相同的哈希码。因此,相等的哈希码只是一个 指示符 两个实例 可能 相等。
Two objects that are equal return hash codes that are equal. However,
the reverse is not true: equal hash codes do not imply object
equality, because different (unequal) objects can have identical hash
codes [...]
You should not assume that equal hash codes imply object equality.
我扩展了 equals 方法和哈希码来检查两个具有布尔属性的相同对象是否相等。当我改变对象使布尔属性之一为 false 而不是 true 时,它无法识别差异并断言它们相等;。有什么想法吗?
public override bool Equals(object value)
{
if (!(value is LocationPropertyOptions options))
return false;
return Equals(options, value);
}
public bool Equals(LocationPropertyOptions options)
{
return options.GetHashCode() == GetHashCode();
}
public override int GetHashCode()
{
return ToString().GetHashCode();
}
public override string ToString()
{
return $"{Identifier}{AccountEntityKey}{Address}{Comments}{Contact}" +
$"{Coordinate}{Description}{FaxNumber}{LastOrderDate}{PhoneNumber}" +
$"{ServiceAreaOverride}{ServiceRadiusOverride}{StandardInstructions}" +
$"{WorldTimeZone_TimeZone}{ZoneField}{CommentsOptions}";
}
您从 value
转换 options
,然后用 options
与 value
调用 Equals
。这意味着您将 value
与 value
进行比较,它 returns 总是 true
适合您
public override bool Equals(object value)
{
if (!(value is LocationPropertyOptions options))
return false;
return Equals(options, value);
}
尝试比较 this
和 value
,例如
return Equals(this, value);
这并没有真正回答你的问题。但是,在实现相等性以避免此类错误时,您应该考虑一些事项。
首先你有两个完全不同的实现来表示相等。您的 override bool Equals(object value)
重定向到静态方法 Object.Equals(object, object)
,它只执行 ReferenceEquals
。另一方面,您的 public bool Equals(LocationPropertyOptions)
(可能是 IEquatable<LocationPropertyOptions>
的实现)只是使用了您奇怪的 GetHashCode
实现,即
第二点:您不应在哈希码实现中使用可变成员,尤其是当您的对象存储在字典或哈希图中时,这在很大程度上取决于哈希码的良好实现。参见 MSDN on GetHashCode
You can override GetHashCode() for immutable reference types. In general, for mutable reference types, you should override GetHashCode() only if:
You can compute the hash code from fields that are not mutable; or
You can ensure that the hash code of a mutable object does not change while the object is contained in a collection that relies on its hash code.
第三个也是最后一个:您不应该在检查相等性时使用 GetHashCode
:
Do not test for equality of hash codes to determine whether two objects are equal
虽然假定相同的对象具有相同的哈希码,但不同的对象可能无论如何都具有完全相同的哈希码。因此,相等的哈希码只是一个 指示符 两个实例 可能 相等。
Two objects that are equal return hash codes that are equal. However, the reverse is not true: equal hash codes do not imply object equality, because different (unequal) objects can have identical hash codes [...]
You should not assume that equal hash codes imply object equality.