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