Strange casting behaviour. Cannot cast object (int) to long

房东的猫 提交于 2019-11-30 04:39:18
svick

Cast from int to long is interpreted as conversion between the two types.

Cast from object to int is interpreted as unboxing a boxed int.

It is the same syntax, but it says two different things.

In the working cases (intlong, object (boxed int)→int), the compiler knows exactly what code to produce. If boxed intlong was to work, the compiler would have to somehow figure out which conversion to use, but it doesn't have enough information to do it.

See also this blog post from Eric Lippert.

The object holds a type int. But it's considered an object (which is a boxed int) and a boxed value type can generally only be cast to its underlying type (the type that is boxed).

To cast it to another type, you first have to cast it to its underlying type. This works:

long longNumber2 = (long) (int) intNumber2;

The reason that var works is that the compiler infers the type at compile time. That means, when you use var, the type of intNumber2 (if you use typeof) will be int. Whereas when you use object, the type will be object.

Using dynamic is a whole different process and cannot be compared with var. Here, the conversion / casting takes place at runtime, using reflection and the DLR library. It will dynamically find the underlying type, find that it has a conversion operator and uses that.

(Caution: Guess)

Int32 has a conversion operator to Int64 which is what gets invoked when you do the first cast. Object doesn't, so your second cast is trying to cast an object to another type which isn't a supertype (Int64 doesn't inherit Int32).

The reason why it works with var is obvious – the compiler just saves you from typing int in that case. With dynamic the runtime does all necessary checks for what needs to be done while normally the compiler would just insert either the cast or invoke the conversion operator.

That it doesn't work due to being two different types of casts (one converting, the other unboxing) has already been stated in answers here. What might be a useful addition, is that Convert.ToInt64() will convert anything that is either a built-in type that can be converted to long, or a type of a class that implements IConvertible.ToInt64(), into a long. In other words, if you want to be able to cast an object that contains an integer (of whatever size) to long, Convert.ToInt64() is the way to go. It is more expensive, but what you are trying to do is more expensive that casting, and the difference is negliable (just big enough to be wasteful in cases where you know the object must be a boxed long).

You need to unbox to the same type that was boxed.

object intNumber2 = 100L;
// or value in the long type range
// object intNumber2 = 9223372036854775806;

long result = (long)intNumber2;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!