Confused by Boxing. Casting -1 to Int64 throws InvalidCastException
Ok I must be overlooking something extremely simple but I am lost.
Given this
object val = -1;
var foo = (Int32)(val);
var bar = (Int64)(val);
The cast to Int64 throws and InvalidCastException.
I recognize this is related to some strangeness with boxing but I don't understand the reasoning.
From what I understand val is boxed as Int32 on the first line.
Then when I try to cast as something other than Int32 InvalidCastException is thrown. I suppose this means that I am trying to unbox val as Int64 when it is actually an Int32?
Still seems strange. Couldn't the cast unbox the value and then try to perform the cast?
Something like (Obviously this is horribly oversimplified, maybe the boxed type isn't known so this isn't possible?):
object val = -1;
Int32 unboxed = (Int32)(val);
var bar = (Int64)(unboxed);
Someone (read: Eric Lippert) School me on the reasoning behind this.
UPDATE: From Eric's Blog that Reed posted a link to this is the succinct answer I was looking for
"...This would be a huge amount of code to generate, and it would be very slow. The code is of course so large that you would want to put it in its own method and just generate a call to it. Rather than do that by default, and always generate code that is slow, large and fragile, instead we’ve decided that unboxing can only unbox to the exact type. If you want to call the slow method that does all that goo, it’s available – you can always call Convert.ToInt32, which does all that analysis at runtime for you. We give you the choice between “fast and precise” or “slow and lax”, and the sensible default is开发者_高级运维 the former. If you want the latter then call the method...."
This is because you can't unbox and perform a conversion in a single operation. You must unbox the Int32 value into an Int32, and then subsequently convert its type.
Because of that, this requires the object to be unboxed, then converted to Int64:
object val = -1;
int foo = (Int32)val;
Int64 bar = (Int64)(Int32)val;
Eric Lippert covered this in detail on his blog post titled Representation and Identity.
精彩评论