Explanation:
Equals()
compares the values of two objects.ReferenceEquals()
compares their references.
For reference types operator==
by default compares references, while for value types it performs (AFAIK) the equivalent of Equals() using reflection.
So. I have a situation where I need to compare two reference types by their values. I can explicitly call Equals()
or I can overload operator==
to perform the desired comparison.
However, overloading operator==
for value comparison kinda-sorta violates the principle of least astonishment. On the other hand explicitly calling two-object Equals
looks like overkill.
What is standard practice here?
I know how to override Equals()
. The question was whether it is commonly acceptable to override operator==
to test for value equality on reference types or whether it is commonly accepted practice to explicitly call Equals/ReferenceEquals to explicitly specify which comparison you want.