Overloading operator== versus Equals()
c#, equals, operator-overloading
Solution
I believe the standard is that for most types, .Equals checks object similarity, and operator `==` checks reference equality.
I believe best practice is that for immutable types, operator `==` should be checking for similarity, as well as `.Equals`. And if you want to know if they really are the same object, use `.ReferenceEquals`. See the C# `String` class for an example of this.
Problem
I'm working on a C# project on which, until now, I've used immutable objects and factories to ensure that objects of type `Foo` can always be compared for equality with `==`. `Foo` objects can't be changed once created, and the factory always returns the same object for a given set of arguments. This works great, and throughout the code base we assume that `==` always works for checking equality. Now I need to add some functionality that introduces an edge case for which this won't always work. The easiest thing to do is to overload `operator ==` for that type, so that none of the other code in the project needs to change. But this strikes me as a code smell: overloading `operator ==` and not `Equals` just seems weird, and I'm used to the convention that `==` checks reference equality, and `Equals` checks object equality (or whatever the term is). Is this a legitimate concern, or should I just go ahead and overload `operator ==`?