StephenCleary / Comparers

The last comparison library you'll ever need!
MIT License
427 stars 33 forks source link

Compare structs #10

Closed djcarter85 closed 6 years ago

djcarter85 commented 6 years ago

Is there a way of automatically implementing the required comparison and equality operators for structs?

StephenCleary commented 6 years ago

Yes!

You cannot use the ComparableBase<T> or ComparableBaseWithOperators<T> with structs, since they cannot have base classes. However, you can use ComparableImplementations, as documented here.

I just checked in some unit tests to show how this is done. This example has a struct that implements IComparable<T> / IEquatable<T> / IComparable (and overrides Equals and GetHashCode). If you want to go a step further and support operators, you can also write those operators out and forward them to ComparableImplementations following that same pattern.