简体   繁体   English

有效的C#:覆盖Object.Equals(),不管是不是?

[英]Effective C#: Overriding Object.Equals(), yay or nay?

In the second edition of Effective C# (ISBN-13: 978-0321658708) on page 37, the book reads 在第37页的Effective C#第二版(ISBN-13:978-0321658708)中,该书内容如下

The second function you'll never redefine is static Object.Equals() 你永远不会重新定义的第二个函数是静态Object.Equals()

However, on page 39, the book reads 但是,在第39页,这本书上写着

The point is that if your type should follow value semantics (comparing contents) instead of reference semantics (comparing object identity), you should write your own override of instance Object.Equals() 关键是如果你的类型应该遵循值语义(比较内容)而不是引用语义(比较对象标识),你应该编写自己的实例Object.Equals()覆盖

Would someone be so kind as to explain why one would override 有人会如此善意地解释为什么会覆盖

public virtual bool Equals(object right);

and not 并不是

public static bool Equals(object left, object right);

Thank you :) 谢谢 :)

因为您无法覆盖静态方法。

The first thing to clear is you cannot override static methods. 要清除的第一件事是你不能覆盖静态方法。 The implementation of 实施

public static bool Equals(object left, object right);

cannot be overridden. 不能被覆盖。 The static equals method is only for connivence for not doing a null check. 静态equals方法仅用于不进行空检查。 Internally it first checks for reference equals and then content equals(non static equals method). 在内部,它首先检查引用等于然后内容等于(非静态等于方法)。

In the quotes you mentioned above, the first quote refers the static equals method where as second refers non static equals method both are written in notation as Object.Equals() but notice that the first one says "static Object.Equals()" 在上面提到的引号中,第一个引用引用静态equals方法,其中第二个引用非静态equals方法,两者都以符号形式写为Object.Equals(),但注意第一个引用“static Object.Equals()”

never redefine static Object.Equals() 永远不会重新定义静态 Object.Equals()

You should write your own override of instance Object.Equals() 你应该编写自己的实例 Object.Equals()的覆盖

Notice the difference, static vs instance . 请注意区别, 静态实例 These sentences are not referring to the same method... 这些句子不是指同一种方法......

另外,如果我没有弄错的话,静态的只会比较参考,其中虚拟的那个使你能够编写自己的比较,大多数时候都是基于值而不是参考。

暂无
暂无

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

相关问题 C#中的Object.Equals澄清? - Object.Equals clarification in C#? 带有多个或条件的 C# Object.Equals() - C# Object.Equals() with multiple or conditions C#:静态object.Equals如何检查是否相等? - C#: How does the static object.Equals check for equality? 如何在 C# “object.equals”中使用“或”(|)运算符 - How do I use “or” (|) operator in C# “object.equals” C#operator ==,StringBuilder.Equals,Object.Equals和Object.ReferenceEquals之间的差异 - C# Differences between operator ==, StringBuilder.Equals, Object.Equals and Object.ReferenceEquals c#中Object.Equals(object,object)和Object.ReferenceEquals(object,object)之间的区别 - difference between Object.Equals(object,object) and Object.ReferenceEquals(object,object) in c# 覆盖C#中的Object.Equals()实例方法;现在代码分析/ FxCop警告CA2218:“还应该重新定义GetHashCode”。我该抑制吗? - Overriding Object.Equals() instance method in C#; now Code Analysis/FxCop warning CA2218: “should also redefine GetHashCode”. Should I suppress this? IEquatable 和只是覆盖 Object.Equals() 有什么区别? - What's the difference between IEquatable and just overriding Object.Equals()? 重写Object.Equals并实现IEquatable &lt;&gt;时无法访问的代码? - Unreachable code when overriding Object.Equals and implementing IEquatable<>? C#运算符重载:Object.Equals(object o)和Object.GetHashCode() - C# operator overloading: Object.Equals(object o) & Object.GetHashCode()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM