简体   繁体   English

c#中Object.Equals(object,object)和Object.ReferenceEquals(object,object)之间的区别

[英]difference between Object.Equals(object,object) and Object.ReferenceEquals(object,object) in c#

I have asked by an Interviewer in an Interview that "difference between Object.Equals(object,object) and Object.ReferenceEquals(object,object)". 我在一次采访中问过一个采访者,问“ Object.Equals(object,object)和Object.ReferenceEquals(object,object)之间的差异”。

I have tried in code snippet but the result is same. 我已经在代码段中尝试过,但是结果是一样的。

Please suggest. 请提出建议。

A a = new A(), b = new A();
MessageBox.Show(""+Object.Equals(a, b));
MessageBox.Show("" + Object.ReferenceEquals(a, b));

Equals is an instance method that takes one parameter (which can be null). Equals是一个采用一个参数(可以为null)的实例方法。 Since it is an instance method (must be invoked on an actual object), it can't be invoked on a null-reference. 由于它是一个实例方法(必须在实际对象上调用),因此不能在空引用上调用它。

ReferenceEquals is a static method that takes two parameters, either / both of which can be null. ReferenceEquals是一个带有两个参数的静态方法,其中两个参数都可以为null。 Since it is static (not associated with an object instance), it will not throw a NullReferenceException under any circumstances. 由于它是静态的(不与对象实例关联),因此在任何情况下都不会引发NullReferenceException

== is an operator, that, in this case (object), behaves identically to ReferenceEquals. ==是一个运算符,在这种情况下(对象),其行为与ReferenceEquals相同。 It will not throw a NullReferenceException either. 它也不会抛出NullReferenceException

To illustrate: 为了显示:

object o1 = null;
object o2 = new object();

//Technically, these should read object.ReferenceEquals for clarity, but this is redundant.
ReferenceEquals(o1, o1); //true
ReferenceEquals(o1, o2); //false
ReferenceEquals(o2, o1); //false
ReferenceEquals(o2, o2); //true

o1.Equals(o1) //NullReferenceException
o1.Equals(o2) //NullReferenceException
o2.Equals(o1) //false
o2.Equals(o2) //true

Object.Equals https://msdn.microsoft.com/en-us/library/w4hkze5k(v=vs.110).aspx Object.Equals https://msdn.microsoft.com/zh-cn/library/w4hkze5k(v=vs.110).aspx

Object.ReferenceEquals https://msdn.microsoft.com/en-us/library/system.object.referenceequals(v=vs.110).aspx Object.ReferenceEquals https://msdn.microsoft.com/zh-cn/library/system.object.referenceequals(v=vs.110).aspx

Acording to this: Object.Equals compares equality of the objects. 与此对应:Object.Equals比较对象的相等性。 Underneath it calls ReferenceEquals and object.Equals(obj). 在其下面调用ReferenceEquals和object.Equals(obj)。

Object.ReferenceEquals compares only references of two objects. Object.ReferenceEquals仅比较两个对象的引用。 It is true only if both refereces point to one object in memory. 仅当两个引用都指向内存中的一个对象时,才为真。

As others have noted, differences only occur if the Equals method is overridden, because the base implementation in object relies on ReferenceEquals . 正如其他人指出的那样,差异仅在Equals方法被覆盖时才会发生,因为object的基本实现依赖于ReferenceEquals

Consider the following example: 考虑以下示例:

public class Person {
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public DateTime Birthdate { get; set; }

    public override bool Equals(object other) {
        var otherPerson = other as Person;
        if (otherPerson == null) {
            return false;
        }
        return Firstname == otherPerson.Firstname
            && Lastname == otherPerson.Lastname
            && Birthdate == otherPerson.Birthdate;
    }
}

Now we create two Persons with the same Name and Birthdate. 现在,我们创建两个具有相同名称和生日的人。 According to our overridden Equals logic, these two are considered the same Person. 根据我们覆盖的Equals逻辑,这两个被视为同一个人。 But for the System, these are two different objects, because they were instantiated twice and therefore the references are not equal. 但是对于系统来说,这是两个不同的对象,因为它们被实例化了两次,因此引用不相等。

var person1 = new Person(Firstname = "John", Lastname = "Doe", Birthdate = new DateTime(1973, 01, 04));

var person2 = new Person(Firstname = "John", Lastname = "Doe", Birthdate = new DateTime(1973, 01, 04));

bool isSameContent = person1.Equals(person2);         // true
bool isSameObject = person1.ReferenceEquals(person2); // false

var person3 = person1;
bool isSameObject2 = person1.ReferenceEquals(person3); // true

If class A overrides method Equals than yo may have difference in results. 如果A类重写方法,则等于yo的结果可能有所不同。 Object.Equals(a, b) use ReferenceEquals as first part of comparision. Object.Equals(a,b)使用ReferenceEquals作为比较的第一部分。 Look here https://msdn.microsoft.com/en-us/library/w4hkze5k(v=vs.110).aspx 在这里查看https://msdn.microsoft.com/zh-cn/library/w4hkze5k(v=vs.110).aspx

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

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