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
Post a Comment