开发者

Long type equality

How is the following possible?

Long type equality

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 objects. You could use .Equals, or declare them as being long.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜