0

I was using the Jon Skeet solution to compare the properties of two complex objects when something weird happened: Those two dates, which looks the same, was failed at the !object.Equals(firstValue, secondValue) test. Grabbing my debugger, I debugged and it seems that the two objects are the very same:

enter image description here

Then I look in the details (pressing the +) and found this:

enter image description here

Could this be causing the Equals() to fails? Or are my eyes shitty and the difference is visible, elsewhere?

Community
  • 1
  • 1
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • 1
    No, a `DateTime` has a `Date` property which returns the `DateTime` without time(so midnight), that `DateTime` of course also has a `Date` property and so on. – Tim Schmelter Mar 24 '15 at 10:12
  • @TimSchmelter then why is the equals failing? – Thomas Ayoub Mar 24 '15 at 10:13
  • 2
    You have provided too less informations to find the reason. I dont know the class definition and i don't see instances. Also, i don't like the reflection approach. Reflection should be the very last resort. – Tim Schmelter Mar 24 '15 at 10:14
  • @TimSchmelter please, tell me what you need, I'll be very glad to add information to unstuck myself – Thomas Ayoub Mar 24 '15 at 10:14
  • 1
    I chose to close this question as a duplicate because you're comparing arrays, not instances of `DateTime`. Arrays are compared by reference, not by the values of their items. – Frédéric Hamidi Mar 24 '15 at 10:15
  • 1
    `firstValue` is a one-dimensional **array** of `DateTime`, having length 1. And `secondValue` is _another_ array. If you use `object.Equals(firstValue, secondValue)` you get `false` because they are different array instances, and arrays have reference-type equality semantics. Did you mean to say instead `object.Equals(firstValue[0], secondValue[0])`? That would give `true` because of the value-type semantics of `DateTime`. Also note that `DateTime` overloads the `operator ==`, and for that reason the usual thing to do is `firstValue[0] == secondValue[0]`. – Jeppe Stig Nielsen Mar 24 '15 at 10:15
  • _[How do I ask a good question?](http://stackoverflow.com/help/how-to-ask)_ –  Mar 24 '15 at 10:15
  • My crystal ball says that you need to remove `!` from the expression. Because their equal. – Hans Passant Mar 24 '15 at 10:16
  • @FrédéricHamidi thanks to point out the **real** issue. Should I delete the question ? – Thomas Ayoub Mar 24 '15 at 10:16
  • @Thomas, that's your choice, but it wouldn't hurt to leave the question around I believe -- it may be useful to others. – Frédéric Hamidi Mar 24 '15 at 10:17
  • @FrédéricHamidi you mean that I'll get downvote but it'll may help someone else ? – Thomas Ayoub Mar 24 '15 at 10:19
  • 1
    @Thomas, well, let's not be too pessimistic -- your question has not been downvoted so far, and may never be (we're all human, and posting duplicates happen). On the other hand, the question might very well help someone else later, yes. – Frédéric Hamidi Mar 24 '15 at 10:21
  • _(removed earlier version of this comment because it contained a fatal typo)_ If you want to compare arrays by entries, you can use `System.Collections.StructuralComparisons.StructuralEqualityComparer.Equals(firstArray, seconArray)`. If the arrays are equal in an entry-wise sense, that gives `true` even if those are distinct instances of `Array`. – Jeppe Stig Nielsen Mar 24 '15 at 11:44

0 Answers0