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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here, you’re calling two different methods –
Double.Equals(double)andObject.Equals(object). For the first call,intis implicitly convertable todouble, so the input to the method is adoubleand it does an equality check between the twodoubles. However, for the second call, theintis 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!