Why does (int)(object)10m throw "Specified cast is not valid" exception?
Why this explicit cast does throw Specified cast is not valid.
exception ?
decimal d = 10m;
object o = d;
int x = (int)o;
But this works:
int x = (int)(decimal)o;
Solution 1:
A boxed value can only be unboxed to a variable of the exact same type. This seemingly odd restriction is a very important speed optimization that made .NET 1.x feasible before generics were available. You can read more about it in this answer.
You don't want to jump through the multiple cast hoop, simple value types implement the IConvertible interface. Which you invoke by using the Convert class:
object o = 12m;
int ix = Convert.ToInt32(o);
Solution 2:
When you do this, you're implicitly boxing the decimal d
to a basic object:
object o = d;
You cannot cast boxed values directly without first unboxing them, which is why casting directly to an int, as in the following, fails:
int x = (int)o;
However, by doing this (intermediately casting to a decimal first):
int x = (int)(decimal)o;
You're first unboxing o
, which means you're retrieving the decimal value, then casting the unboxed decimal value to an int, which works because C# supports casting decimals to ints.