I try to understand this code:
double b = 3;
object o = b;
Console.WriteLine(o.Equals(3));//false
Console.WriteLine(o.Equals(b));//true
Console.WriteLine( o == (object)b );//false
- Each new boxing makes different references of object b?
- If 1. is true, why
o.Equals(b)istrue? - If
Equalsdoes not check references, whyo.Equals(3)isfalse?
Thanks.
Equalscheck for value equality, not reference equality. Bothoandbare the same: adoublewith a value of3.0.3here is anint, not adouble, andEqualsfor different types doesn’t do any conversion to make them compatible, like the compiler is usually doing.o.Equals(3.0)will returntrue.