class A { /* members */ };
A one, two;
if (one == two) { /* they're equal */ }
will not compile. This is good; you know you need to implement an operator ==() to proceed.
And now C# is my mainstay, partly of necessity, and partly (although I hate to admit it) because it is a productivity booster. So... what do you know, this compiles:
class A { /* members */ }
A one = new A();
A two = new A();
if (one == two) { /* they're equal...? */ }
But how does it assess the equality of the two objects? Here's the fun bit: for object variables in C# that do not provide their own, operator ==() generally compares the references for equality. Thus it will only return true if both variables refer to the same instance of the class (the equivalent of comparing the addresses of the C++ objects, like &one == &two). Surprise! And here I thought there would be some clever memberwise comparison going on.
I use Dictionary<K,V> a lot, even for my own classes as keys. But read the Dictionary docs carefully: you'll need to implement IEquatable<T> in your classes if you properly want to use them as keys! And if you read the IEquatable<T> docs carefully, you'll see you should really implement three member functions:
bool Equals(T rhs)
virtual override bool Equals(object rhs)
virtual override int GetHashCode()
That was definitely one of those "ya learn something new every day" days. Such fun.
