When you call Object.Equals(o1, o2) the static version, internally that function first checks to see if the references are not null, and then calls the virtual method o1.Equals(o2). I just checked that using Reflector so that's accurate. Now here's where it can get tricky, if you do not override the Equals method, when o1.Equals(o2) is called in the static function, it just checks object references. I'm going to repost the code that I previously posted.namespace PersonTest { class Program { static void Main(string[] args) { Person p1 = new Person { Member = "ABC", OtherMember = 1 }; Person p2 = new Person { Member = "ABC", OtherMember = 1 }; Console.WriteLine(Object.Equals(p1, p2).ToString()); Console.WriteLine(Object.ReferenceEquals(p1, p2).ToString()); Console.ReadLine(); } } public class Person { public String Member { get; set; } public Int32 OtherMember { get; set; } public override Boolean Equals(Object obj) { return obj is Person ? Equals(obj as Person) : false; } private Boolean Equals(Person obj) { if (Object.ReferenceEquals(this, obj)) return true; if ((obj == null) || (this == null)) return false; return obj.Member == this.Member && obj.OtherMember == this.OtherMember; } public override int GetHashCode() { return base.GetHashCode(); } } }
Put this into a project and the output will be True False. Then comment out both of the Equals methods in the Person class and the output will be False False. The reason is that you have 2 different references to Person classes. Regardless of the EQUALITY of the classes the IDENTITY will always be different. Object.ReferenceEquals is always IDENTITY. Object.Equals starts by checking EQUALITY, but then defaults to IDENTITY in the absence of an overridden Equals() method in your class. That's why when you comment out the Equals methods in the Person class, the Object.Equals(p1, p2) the method goes to IDENTITY and not EQUALITY. You have to supply the EQUALITY. I hope this rather thorough explanation helps. Scott
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand