简体   繁体   English

C#:静态object.Equals如何检查是否相等?

[英]C#: How does the static object.Equals check for equality?

Say you have two different classes where each have their own implementation of Equals; 假设你有两个不同的类,每个类都有自己的Equals实现; which one is used? 使用哪一个? What if only one of them have one? 如果只有一个人有一个怎么办? Or none of them? 或者他们都没有? Are any of the following lines equivalent? 以下任何一行是否相同?

object .Equals( first, second )
first .Equals( second )
second .Equals( first )

I'm guessing that the first two might be equivalent, but I don't really have a clue. 我猜测前两个可能是等价的,但我真的不知道。

What does it really do? 它到底是做什么的?

Basically it does three things: 基本上它做了三件事:

  • Check for reference equality (return true if so) 检查引用相等性(如果是,则返回true)
  • Check for reference nullity (return false if either value is null; by now the null == null case has been handled) 检查引用是否为null(如果任一值为null,则返回false;现在已经处理了null == null的情况)
  • Check for value equality with first.Equals(second) 检查值是否与first.Equals(second)相等

The ordering shouldn't matter if both values have well-behaved equality implementations, as equality should be implemented such that x.Equals(y) implies y.Equals(x) . 排序不应该的问题,如果这两个值都乖巧平等的实现,作为平等应该被实现,使得x.Equals(y)意味着y.Equals(x) However, the offline documentation I've got installed does state that first.Equals(second) (or objA.equals(objB) to use the real parameter naming) is specified. 但是,我安装的脱机文档确实指出first.Equals(second)(或objA.equals(objB)使用真实参数命名)。 The online documentation doesn't mention this, interestingly enough. 有趣的是, 在线文档没有提到这一点。

Just to make all of this concrete, the implementation could look like this: 为了使所有这些具体,实现可能如下所示:

public static bool Equals(object x, object y)
{
    if (x == y) // Reference equality only; overloaded operators are ignored
    {
        return true;
    }
    if (x == null || y == null) // Again, reference checks
    {
        return false;
    }
    return x.Equals(y); // Safe as we know x != null.
}

By default, object equivalency is determined by the object's address in memory. 默认情况下,对象等效性由对象在内存中的地址决定。 If both instances have the same memory address, they are equal. 如果两个实例具有相同的内存地址,则它们是相等的。

However, this can be overloaded within the object so that developers can compare two objects that arn't in the same memory location and still be considered equal. 然而,这可以在对象内被重载以使得开发者可以比较arn't在相同的存储器位置,并且仍然被认为是相等的两个对象。 For example, if you had a Data Access Layer where each object had its data record's ID from the database, you could have object equality compared based on the ID. 例如,如果您有一个数据访问层,其中每个对象都有来自数据库的数据记录ID,则可以根据ID比较对象相等性。

You can overload operators to produce this functionality. 您可以重载运算符以生成此功能。

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

相关问题 如何在 C# “object.equals”中使用“或”(|)运算符 - How do I use “or” (|) operator in C# “object.equals” C#中的Object.Equals澄清? - Object.Equals clarification in C#? 带有多个或条件的 C# Object.Equals() - C# Object.Equals() with multiple or conditions Object.Equals是虚拟的,但是Object.operator ==不在C#中使用它吗? - Object.Equals is virtual, but Object.operator== does not use it in C#? obj1.Equals(obj2)和c#中的静态Object.Equals(obj1,obj2)有什么区别? - What's the difference between obj1.Equals(obj2) and static Object.Equals(obj1, obj2) in c#? 有效的C#:覆盖Object.Equals(),不管是不是? - Effective C#: Overriding Object.Equals(), yay or nay? C#Equals扩展程序无法检查相等性 - C# Equals extension is unable to check equality 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(object o)和Object.GetHashCode() - C# operator overloading: Object.Equals(object o) & Object.GetHashCode()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM