1

I have asked by an Interviewer in an Interview that "difference between Object.Equals(object,object) and 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));
  • is A overriding Equals methods? that might make some difference – slawekwin Jan 20 '16 at 09:20
  • Have you tried to read [this](https://msdn.microsoft.com/en-us/library/system.object.referenceequals(v=vs.110).aspx) and [this](https://msdn.microsoft.com/en-us/library/w4hkze5k(v=vs.110).aspx)? – Farhad Jabiyev Jan 20 '16 at 09:20
  • Or just look at the source to see the difference: http://referencesource.microsoft.com/#mscorlib/system/object.cs,d9262ceecc1719ab – Dennis_E Jan 20 '16 at 09:24
  • 2
    Object class have two Overloaded method 'Equals' one has one parameter and that one is instance method. But i am talking about another Equals method that takes two parameter and also static because it calls by using Object class name and because of static it cant be override. – Mohd Ismail Siddiqui Jan 20 '16 at 09:27
  • Dennis_E: thanks for your response, but ithink there is some porblem public static bool Equals(Object objA, Object objB) { if (objA==objB) { return true; } if (objA==null || objB==null) { return false; } return objA.Equals(objB); } When i try this equals method with both object have null values it returns me true value that oppose above method definition. – Mohd Ismail Siddiqui Jan 20 '16 at 09:30
  • One way to see a difference, is to use value-types that will be boxed late, for example `Object.Equals(42, 42)` versus `Object.ReferenceEquals(42, 42)`. Another way to see a difference is to use a reference type that overrides the other `Equals` method (the `Equals(object obj)` one), for example `Version a = new Version("1.0.1"); Version b = new Version("1.0.1");` followed by tests of `Object.Equals(a, b)` and `Object.ReferenceEquals(a, b)`. – Jeppe Stig Nielsen Jan 20 '16 at 09:41

4 Answers4

2

As others have noted, differences only occur if the Equals method is overridden, because the base implementation in object relies on 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. 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
Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36
2

Equals is an instance method that takes one parameter (which can be 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. Since it is static (not associated with an object instance), it will not throw a NullReferenceException under any circumstances.

== is an operator, that, in this case (object), behaves identically to ReferenceEquals. It will not throw a NullReferenceException either.

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
  • No. The `Equals` method in question is **not** an instance method that takes one parameter. Take a look again. Also, see [MSDN](https://learn.microsoft.com/en-us/dotnet/api/system.object.equals?view=netcore-3.1#System_Object_Equals_System_Object_System_Object_). – l33t May 15 '20 at 09:53
1

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

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

Acording to this: Object.Equals compares equality of the objects. Underneath it calls ReferenceEquals and object.Equals(obj).

Object.ReferenceEquals compares only references of two objects. It is true only if both refereces point to one object in memory.

Alex Lebedev
  • 601
  • 5
  • 14
0

If class A overrides method Equals than yo may have difference in results. Object.Equals(a, b) use ReferenceEquals as first part of comparision. Look here https://msdn.microsoft.com/en-us/library/w4hkze5k(v=vs.110).aspx

fryday
  • 387
  • 2
  • 14
  • Object class have two Overloaded method 'Equals' one has one parameter and that one is instance method. But i am talking about another Equals method that takes two parameter and also static because it calls by using Object class name and because of static it cant be override. – Mohd Ismail Siddiqui Jan 20 '16 at 09:34
  • @MohdIsmailSiddiqui The overload with two parameters, `Equals(object, object)`, will call on to the non-static overload with one parameter `Equals(object)` in most cases. – Jeppe Stig Nielsen Jan 20 '16 at 09:37
  • I wrote about overriding method Equals in class **A**. Not about overriding static method. Look link in my answer. – fryday Jan 20 '16 at 09:37
  • have you observe that i have used Object.Equals not a.Equals. Please look into the code again. – Mohd Ismail Siddiqui Jan 20 '16 at 09:39
  • The static Equals(Object, Object) method indicates whether two objects, objA and objB, are equal. It also enables you to test objects whose value is null for equality. It compares objA and objB for equality as follows: – fryday Jan 20 '16 at 09:40
  • It determines whether the two objects represent the same object reference. If they do, the method returns true. This test is equivalent to calling the ReferenceEquals method. In addition, if both objA and objBare null, the method returns true. It determines whether either objA or objB is null. If so, it returns false. – fryday Jan 20 '16 at 09:40
  • 1
    If the two objects do not represent the same object reference and neither is null, it calls objA.Equals(objB) and returns the result. This means that if objA overrides the Object.Equals(Object) method, this override is called. – fryday Jan 20 '16 at 09:41
  • Thats why method a.Equals may be called in some cases! – fryday Jan 20 '16 at 09:41
  • Interresting that `Object.Equals(a, a)` where `a` is a reference type which is not null, "short-circuits" and returns `true` immediately without asking `a.Equals(a)`. This is different from `EqualityComparer.Default.Equals(a, a)` which goes on to ask the object itself even if the references point to the same instance. – Jeppe Stig Nielsen Jan 20 '16 at 09:59