What's going on here?
int zero = 0;
double x = 0;
object y = x;
Console.WriteLine(x.Equals(zero)); // True
Console.WriteLine(y.Equals(zero)); // False
What's going on here?
int zero = 0;
double x = 0;
object y = x;
Console.WriteLine(x.Equals(zero)); // True
Console.WriteLine(y.Equals(zero)); // False
Here, you're calling two different methods -
Double.Equals(double)
andObject.Equals(object)
. For the first call,int
is implicitly convertable todouble
, so the input to the method is adouble
and it does an equality check between the twodouble
s. However, for the second call, theint
is not being cast to adouble
, it's only being boxed. If you have a look at theDouble.Equals(object)
method in reflector, the first line is:so it's returning false, as the input is a boxed
int
, not a boxeddouble
.Good catch!