Mohd Ismail Siddiqui
Mohd Ismail Siddiqui

Reputation: 678

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)".

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));

Upvotes: 1

Views: 2074

Answers (4)

Ankit Misra
Ankit Misra

Reputation: 99

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

Upvotes: 2

Georg Patscheider
Georg Patscheider

Reputation: 9463

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

Upvotes: 2

Alex Lebedev
Alex Lebedev

Reputation: 609

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.

Upvotes: 2

fryday
fryday

Reputation: 387

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

Upvotes: 0

Related Questions