Case 1 produces a type mismatch exception. Case 2 works as expected. Does anyone have insight into why? Or a better way to have converted from Int32 as an object into an Int16?
Case 1:
var i = (Int16)(object)Int32.Parse("1");
Case 2:
var i = (Int16)Int32.Parse("1");
The underlying cause is the fact that in C# the explicit cast operator has two different meanings:
So the reason you're getting two different behaviors is that you are using each of the above different meanings respectively:
object
is definitely a boxed Int16
and I'd like to use it as such". But since it's actually a boxed Int32
, you get a type mismatch exception.Int32
is definitely not an Int16
, but I'd like to convert it to one".In other words, unboxing works only if you attempt to unbox to the original type. According to Eric Lippert, the reason is that it was simply too impractical to implement it in a way that it could unbox and convert in a single operation.
As Damian correctly commented, I was wrong. The problem wasn't the conversion, the problem lies in the unboxing operation. It only allows unboxing from an object from the same type it was originally boxed from.
int > object > int
is okay, int > object > short
isn't and so is short > object > int
.
This will only work if the int
was first converted to a short, like so:int > short > object > short
is fine.
This particular case is even used in an unboxing sample on MSDN.
In your first case your are trying to unbox
an Int32
typed value into a Int16
typed field, which gives you the type-mismatch exception since there is no implicit conversion available to cast from object
to Int32
.
In your second case you are directly casting an Int32
to an Int16
. Since this is a direct cast here you'll get the benefit of implicit type conversion
(see this MSDN article for a more detailed explanation).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With