One of the pitfalls of doing development in C#, Java, C++, or really any predominantly Object Oriented language (OOP) is how “equality” is defined.
In C#, for instance, you have the following methods that are built into every object
:
object.Equals
- the
==
operator
ReferenceEquals
, for explicitly checking reference equality
My personal opinion: in any managed language, checking for referential equality is a pretty bad default - and if you’re working with immutable objects then equality by reference doesn’t really work.
In C/C++, where pointer arithmetic and knowing the precise location of something in memory matters it’s a different story. Equality by reference is the correct default in that case.
What’s the right thing to do?