I don't think I understand why the first statement evaluates to true
and the last statement evaluates to false
, but it's been a long day.
Can somebody explain it?
0L.Equals(0) // true
((object)0L).Equals(0L) // true
((object)0L).Equals(0) // false
You cannot compare them in this way because the new Long(0) returns a reference to a class, not a primitive type.
longs can not be compared with ==. The theory states that you need to use a simple trick like subtracting the one from the other and see the relevant difference.
Object.Equals
first compares the types if the object is a value type which it is. Both are different in this case.
MSDN:
If the current instance is a value type, the Equals(Object) method tests for value equality. Value equality means the following: The two objects are of the same type. As the following example shows, a Byte object that has a value of 12 does not equal an Int32 object that has a value of 12, because the two objects have different run-time types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With