0

I've implemented an IEqualityComparer<DateTime> which can compare two dates based on a tolerance value in milliseconds. If the difference between the two is less than the tolerance, they should be treated as equal.

The Equals side is almost trivial to implement, however I have really no idea how I could implement the GetHashcode to cooperate with the equality comparison.

The problem ccan be generalized for numbers as well: given two double and a delta value, a similar equality comparer could be implemented and the similar GetHashcode difficulties would arise.

Any idea would be appreciated.

EDIT

I know such an equality comparer is kind of tricky because such an equality operator would not be a transitive relation for example. Still, I'd love to hear some possible ideas or alternatives.

Zoltán Tamási
  • 12,249
  • 8
  • 65
  • 93
  • `return 0;` gets the job done. Just don't create a large Dictionary or HashSet with that class as a key, performance might be a bit disappointing. – Hans Passant May 04 '17 at 21:00
  • Thank you for the tip. That's my current implementation, and it works indeed. I was just looking for a more proper way,. – Zoltán Tamási May 05 '17 at 15:03

0 Answers0