Your approach would end up invoking the virtual Equals(Object)
method, because that's what Object.Equals
does. The way that's given here only uses Object.Equals
when at least one of the values is null, in order to return true
if they're both null and false otherwise - that path will never call back into your Equals
implementations.
If both values are non-null, the MSDN approach will make a non-virtual call straight to bool Equals(Person)
- which avoids not only a virtual method indirection, but also another type check which is redundant when we already know that if the references are non-null, they're both Person
references.
I think the example is unfortunate in its mixture of using fields and properties, mind you - and I'd definitely write this:
if (this.uniqueSsn == other.SSN)
return true;
else
return false;
as just:
return this.uniqueSsn == other.SSN;
or more obviously:
return this.uniqueSsn == other.uniqueSsn;