简体   繁体   English

C#Equals扩展程序无法检查相等性

[英]C# Equals extension is unable to check equality

I extended the equals method and hashcode to check for equality of two identical objects with boolean properties. 我扩展了equals方法和哈希码,以检查两个具有布尔属性的相同对象的相等性。 when I mutate the object making one of the boolean properties false instead of true it fails to recognize the difference and asserts they are equal;. 当我更改对象,使布尔属性之一为false而不是true时,它无法识别差异并断言它们相等; Any Ideas why? 任何想法为什么?

   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}";
    }

You cast options from value , then call Equals with options vs value . 您从value options ,然后通过options vs value调用Equals That means you compare value with value , it returns always true for you 这意味着您将valuevalue进行比较,它对您始终返回true

public override bool Equals(object value)
{
    if (!(value is LocationPropertyOptions options))
       return false;    
    return Equals(options, value);
}

Try comparing this with value , like 尝试将thisvalue进行比较,例如

return Equals(this, value);

This doesn´t really answer you question. 这并不能真正回答您的问题。 However you should consider a few things when implementing equality to avoid this kind of error. 但是,在实现相等性时应考虑一些事情,以避免此类错误。

First you have two completely different implementations to indicate equality. 首先,您有两个完全不同的实现来表示相等。 Your override bool Equals(object value) redirects to the static method Object.Equals(object, object) , which just performs a ReferenceEquals . 您的override bool Equals(object value)重定向到静态方法Object.Equals(object, object) ,该方法仅执行ReferenceEquals Your public bool Equals(LocationPropertyOptions) (probably an implementation for IEquatable<LocationPropertyOptions> ) on the other hand simply uses your strange GetHashCode -implementation, which is 另一方面,您的public bool Equals(LocationPropertyOptions) (可能是IEquatable<LocationPropertyOptions> )仅使用您奇怪的GetHashCode -implementation,即

Point two: you should not use a mutable member within your hashcode-implementation, in particular when your objects are stored within a dictionary or a hashmap, which heavily depends on a good implementaion for a hashcode. 第二点:您不应该在哈希码实现中使用可变成员,尤其是当您的对象存储在字典或哈希图中时,这在很大程度上取决于哈希码的良好实现。 See MSDN on GetHashCode GetHashCode查看MSDN

You can override GetHashCode() for immutable reference types. 您可以重写GetHashCode()以获取不可变的引用类型。 In general, for mutable reference types, you should override GetHashCode() only if: 通常,对于可变引用类型,仅在以下情况下才应覆盖GetHashCode():

  • 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. 您可以确保在对象包含在依赖于其哈希代码的集合中时,该可变对象的哈希代码不会更改。

Third and last: you shouldn´t use GetHashCode in your check for equality: 第三,也是最后一个:您不应在检查是否相等时使用GetHashCode

Do not test for equality of hash codes to determine whether two objects are equal 不要测试哈希码是否相等,以确定两个对象是否相等

Whilst equal objects are assumed to have identical hashcodes, different objects may have the exact same hashcode anyway. 尽管假定相等的对象具有相同的哈希码,但无论如何,不​​同的对象可能具有完全相同的哈希码。 An equal hashcode therefor is just an indicator that two instances may be 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. 您不应假定相等的哈希码表示对象相等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM