简体   繁体   English

重写Object.Equals并实现IEquatable <>时无法访问的代码?

[英]Unreachable code when overriding Object.Equals and implementing IEquatable<>?

I'm a litte bit confused right now. 我现在有点困惑。 From my understanding the .NET runtime will pick the overloaded method that best suits the given parameter's type. 根据我的理解,.NET运行时将选择最适合给定参数类型的重载方法。 So I would think, that in the snippet below the method Equals(object obj) will never be called with an instance of type Entry . 所以我认为,在方法Equals(object obj)下面的代码段中,永远不会使用Entry类型的实例来调用它。 Instead - because there exists the method Equals(Entry other) that has the correct parameter type - it will be called. 相反-因为存在方法Equals(Entry other)具有正确的参数类型-它将被调用。

The documentation for IEquatable on MSDN states that MSDN上的IEquatable文档指出:

If you implement IEquatable, you should also override the base class implementations of Object.Equals(Object) and GetHashCode so that their behavior is consistent with that of the IEquatable.Equals method. 如果实现IEquatable,则还应该重写Object.Equals(Object)和GetHashCode的基类实现,以使它们的行为与IEquatable.Equals方法的行为一致。 If you do override Object.Equals(Object), your overridden implementation is also called in calls to the static Equals(System.Object, System.Object) method on your class. 如果您确实重写Object.Equals(Object),则在对类上的静态Equals(System.Object,System.Object)方法的调用中也会调用重写的实现。

My questions are: 我的问题是:

  1. Is it correct, that the Equals(object obj) method below will never be called with an instance of type Entry ? 是正确的,永远不会使用Entry类型的实例调用下面的Equals(object obj)方法吗?
  2. Wouldn't it then be enough to just return false in the Equals(object obj) method below? 那么在下面的Equals(object obj)方法中return false是否就足够了?
  3. If so, why doesn't the compiler recognize the commented line below as unreachable? 如果是这样,编译器为何不将下面的注释行视为无法访问?

The code I refer to: 我引用的代码:

sealed class Entry : IEquatable<Entry> {
    ...
    // Overrides Object.Equals
    public override bool Equals(object obj)
    {
        if (obj is Entry)
        {
            return this.Equals(obj as Entry); // Is this code reachable?
        }
        return false;
    }

    // Implements IEquatable<Entry>.Equals
    public bool Equals(Entry other)
    {
        return this.Hash.Equals(other.Hash)
            && this.Path.Equals(other.Path)
            && this.Order.Equals(other.Order);
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }
}

Thanks in advance for helping me! 在此先感谢您的帮助!

Is it correct, that the Equals(object obj) method below will never be called with an instance of type Entry? 是正确的,永远不会使用Entry类型的实例调用下面的Equals(object obj)方法吗?

No. Consider: 不,请考虑:

object entry1 = new Entry(...);
object entry2 = new Entry(...);
bool equal = entry1.Equals(entry2);

The compile-time type of entry2 is object , not Entry , so this will still call Equals(object) . entry2的编译时类型是object ,而不是Entry ,因此仍将调用Equals(object)

(Note that your GetHashCode implementation is decidedly dodgy, by the way - and you're currently not guarding against nulls anywhere. It doesn't help that we don't know whether Entry is a class or a struct.) (顺便说一下,请注意,您的GetHashCode实现绝对是狡猾的,并且您目前无法在任何地方防止null。我们不知道Entry是类还是结构也无济于事。)

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

相关问题 IEquatable 和只是覆盖 Object.Equals() 有什么区别? - What's the difference between IEquatable and just overriding Object.Equals()? Assert.AreEquals调用object.Equals而不管IEquatable实现如何 - Assert.AreEquals calls object.Equals irrespective of IEquatable implemenations 重写Object.Equals时,是否适合使用传入的对象的Equals(MyType)? - When overriding Object.Equals, is it appropriate to use the passed in object's Equals(MyType)? 测试对象相等性的最佳方法是什么 - 不重写Equals和GetHashCode,或实现IEquatable <T> ? - What is the best way to test for object equality - without overriding Equals & GetHashCode, or implementing IEquatable<T>? IEquatable上的重载等于 - Overriding Equals on IEquatable 用于Object.Equals的IL代码生成 - IL code generation for Object.Equals IEquatable之间的差异 <T> ,IEqualityComparer <T> ,并在自定义对象集合上使用LINQ时覆盖.Equals()? - Differences between IEquatable<T>, IEqualityComparer<T>, and overriding .Equals() when using LINQ on a custom object collection? 有效的C#:覆盖Object.Equals(),不管是不是? - Effective C#: Overriding Object.Equals(), yay or nay? IEquatable - 覆盖等于 - 检查空值 - IEquatable - Overriding Equals - checking for nulls Object.Equals的奇怪实现 - Strange implementation of Object.Equals
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM