Long type equality
How is the following possible?
We have two longs with equal vl开发者_JS百科aues but they are not equal...
The compile-time type of the expression is object
, not long
. Therefore ==
is comparing the references. It's like this:
object x = 9L;
object y = 9L;
Console.WriteLine(x == y); // false
Console.WriteLine(x.Equals(y)); // true
Console.WriteLine(object.Equals(x, y)); // true; avoids NullReferenceException
You have two distinct objects, both being "boxes" for the long values. ==
on object
only compares whether the references refer to the exact same object. Equals
compares the objects with each other for value equality, so will return true.
If the compile-time types of the two expressions were long
, ==
would compare them as long values and that would be fine.
At a guess, I'd say you are comparing two objects, and not two integers. The objects are not pointing to the same memory address, thus are not equal.
If you do a Convert.ToInt64(otherRouteValue) == Convert.ToInt64(RouteKeyValue.Value)
you should get the desired result
==
compares the objects (return true if there are the same objects, same references).
To evaluate an equality between object values, use obj.Equals(obj)
method.
http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx
In your case : routeKeyValue.Value.Equals(otherRouteKeyValue);
Because it are object
s. You could use .Equals, or declare them as being long
.
精彩评论