Benefits to IEquatable?

By default, structs have default methods for Equals and GetHashCode. The default implementations match what VS generates when selecting the autofix option.

Instead of implementing this interface on every struct just to use a == b, does it make more sense to just use a.equals(b)?

Comparing the two:

  • .equals is a bit longer to type out, but equally readable.
  • writing less boilerplate helps maintain development velocity.
  • relying on the default implementations avoids churn when adding new fields to the struct.

Thoughts?

(PS: Thanks for the great course!)

Technically yes I believe that would work exactly the same although I really prefer using == as opposed to Equals();
I don’t think development velocity is really a concern, it takes a few minutes to implement which isn’t really a significant amount of time in the long run.
Same thing for the “issue” of having to refactor when adding more things to the struct, technically that is correct but the struct is pretty basic so not something that is frequently modified after it’s done.

So yup you could just use Equals() but I really think taking the small amount of time to make == work is more than worth it since it makes the entire rest of the development much more intuitive.

1 Like

Bear in mind as well that demonstrating how to implement the == operator (and by extension, other operators) increases your skill set for times when these operators are absolutely needed.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms