Suppose I have the following, completely pointless code:
object val1 = 1;
object val2 = 1l;
The below will return false, because the two boxed objects aren’t the same, as objects they have different types:
val1 == val2
The same applies for:
val1.Equals(val2)
Or:
Object.Equals(val1, val2)
What is the easiest way to compare these in the same way that the following would, considering I don’t know the types at runtime (and hence cannot cast):
1 == 1l;
In other words, how do I get a value based comparison on two boxed numbers?
Try using the dynamic keyword. It will resolve the objects to your value types so that you can use the == operator and compare the true values: