Even if I know how / when to overload the == operator, WHY should I do this?

Assuming I am developing a collection of objects for others to use and assuming that I have read and understood (yes?) Most of the threads about the differences between == and Equals () operators, WHY should I ever implement the == operator?

Stealing an example from this thread using it can be quite error prone:

object x = "hello"; object y = 'h' + "ello"; // ensure it a different reference (x == y); // evaluates to FALSE string x = "hello"; string y = 'h' + "ello"; // ensure it a different reference (x == y); // evaluates to TRUE 

So, can I tell my users to just use Equals () and ReferenceEquals () and what is it? What am I missing there?

  • Are there fragments of the standard code base that use ==, and is there no way around this?

  • Is == performance much better in some important cases? (well, well, Equals is virtual, so it will be a little slower all the time, but I don't see any use case where this actually becomes a bottleneck)

  • something else?
+7
source share
4 answers

You can tell your users to simply use Equals ... but it’s very convenient for me that various types (including String ) overload == . Yes, you need to know how overload resolution works, but for most experienced developers, I suspect that this is not a problem most of the time, whereas:

 if (Equals(x, y)) 

looks more unpleasant than

 if (x == y) 

in my opinion. Please note that they are both different from

 if (x.Equals(y)) 

which will explode if x is zero, of course ...

If you prefer not to overload == , then it doesn’t seem like something will fail - you simply can get disgruntled users.

+8
source

You implement == as a convenience to users of your API. If you feel so much that your API users will be confused by it, then do not implement it. Personally, I would not agree with you, but this is your decision at the end of the day.

+2
source

WHY should I implement operator== ?

For the convenience of users of your type. If you are the only user and you are happy to always check for equality by invoking Equals , you do not need to.

But if you implemented, say, the complex type of number 1, I, as a consumer of your type, would be extremely disappointed to find that I could not do this:

 var z = new Complex(1, 0); var c = new Complex(1, 0); var eq = z == c; 

but intead had to say

 var eq = Complex.Equals(z, c); 

1 I know that there is one ...

0
source

For me, this is a question about what I usually want to do with objects.

Take the lines, for example. In 99% of all cases, you are comparing two lines to check for equality, not whether both variables point to the same line.

You also write:

 int x, y; if( x == y ) .. 

So why do you want to force yourself to use:

 BigNum x, y; if( x.Equals(y) ) ... 

Of course, this is a source of errors (like most operator overloads), but I think that taking into account the correct context (for example, an object where it is obvious that you want to compare its value), then this is a good thing for overloading. If it is ambiguous, then I will always stick to Equals () and not overload ==.

0
source

All Articles