c# - Why does casting the same two numbers to Object make them not equal? -


i have following code snippet getting wrong output.

class program     {         static void main(string[] args)         {             var = 10000;             var j = 10000;             console.writeline((object) == (object) j);           }     } 

i expecting true getting false

you boxing numbers (via object cast) creates new instance each variable. == operator objects based on object identity (also known reference equality) , seeing false (since instances not same)

to correctly compare these objects use object.equals(i, j) or i.equals(j). work because actual runtime instance of object int32 equals() method has correct equality semantics integers.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -