覆盖 Object.Equals 和实现 IEquatable<> 时无法访问代码?
Unreachable code when overriding Object.Equals and implementing IEquatable<>?
我现在有点困惑。根据我的理解,.NET 运行时将选择最适合给定参数类型的重载方法。所以我想,在下面的代码片段中,方法 Equals(object obj)
永远不会被类型 Entry
的实例调用。相反 - 因为存在具有正确参数类型的方法 Equals(Entry other)
- 它将被调用。
documentation for IEquatable
on MSDN 表示
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. 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.
我的问题是:
- 下面的
Equals(object obj)
方法永远不会用 Entry
类型的实例调用,对吗?
- 在下面的
Equals(object obj)
方法中仅 return false
就足够了吗?
- 如果是这样,为什么编译器不将下面的注释行识别为不可访问?
我参考的代码:
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();
}
}
在此先感谢您对我的帮助!
Is it correct, that the Equals(object obj) method below will never be called with an instance of type Entry?
没有。考虑:
object entry1 = new Entry(...);
object entry2 = new Entry(...);
bool equal = entry1.Equals(entry2);
entry2
的编译时类型是object
,不是Entry
,所以这仍然会调用Equals(object)
。
(请注意,您的 GetHashCode
实现绝对是狡猾的,顺便说一下 - 您目前没有在任何地方防止空值。我们不知道 Entry
是 class 或结构。)
我现在有点困惑。根据我的理解,.NET 运行时将选择最适合给定参数类型的重载方法。所以我想,在下面的代码片段中,方法 Equals(object obj)
永远不会被类型 Entry
的实例调用。相反 - 因为存在具有正确参数类型的方法 Equals(Entry other)
- 它将被调用。
documentation for IEquatable
on MSDN 表示
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. 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.
我的问题是:
- 下面的
Equals(object obj)
方法永远不会用Entry
类型的实例调用,对吗? - 在下面的
Equals(object obj)
方法中仅return false
就足够了吗? - 如果是这样,为什么编译器不将下面的注释行识别为不可访问?
我参考的代码:
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();
}
}
在此先感谢您对我的帮助!
Is it correct, that the Equals(object obj) method below will never be called with an instance of type Entry?
没有。考虑:
object entry1 = new Entry(...);
object entry2 = new Entry(...);
bool equal = entry1.Equals(entry2);
entry2
的编译时类型是object
,不是Entry
,所以这仍然会调用Equals(object)
。
(请注意,您的 GetHashCode
实现绝对是狡猾的,顺便说一下 - 您目前没有在任何地方防止空值。我们不知道 Entry
是 class 或结构。)