c# - Why don't integers get lifted to doubles when treated as objects and comparing? -


i ran across interesting situation today:

var = new hashset<object> { 1.0, 2.0, 3.0 }; a.contains(1);     //false a.contains(1.0);   //true 

of course, generic version of this:

object b = 2.0; b.equals(2);            //false b.equals(2.0);          //true 

i realize reason because if write 2.0 == 2, c# compiler secretly inserts cast integer double, , using object intermediate, compiler doesn't have enough information this.

my question is, doesn't runtime have enough information lift integer double comparison? if c# compiler assumes it's desirable enough have implicit conversion, why shouldn't jit have similar behavior?

c# has work way language specification says works. has nothing jitter, has implement language specification.

the c# language specification says how == must work.

the clr specification says how equals() must work.

there interesting change made between .net 1.1 , .net 2.0.

in .net 1.1, 3f.equals(3) == false.

in .net 2.0, 3f.equals(3) == true.

this not same object comparing version of equals(). shows how subtle kind of thing is.

an interesting (but old) blog here: http://blogs.msdn.com/b/jmstall/archive/2005/03/23/401038.aspx

it have few details relate question, it's worth read.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -