Consider this code:
private static void Main(string[] args)
{
short age = 123;
object ageObject = age;
//var intAge = (int)ageObject;//Specified cast is not valid.
int newAge= (short)intAge;
Console.ReadLine();
}
I have to assign a short value to object and again cast to int, but when I try to this: var intAge = (int)ageObject;
I get : Specified cast is not valid. I don't know why?
After search in google i found that should cast to short and assign to int:int newAge= (short)intAge;
Why we should casting to short and assign to int?
Why compiler has this behavior?
[http://stackoverflow.com/questions/3953391/why-does-intobject10m-throw-specified-cast-is-not-valid-exception](http://stackoverflow.com/questions/3953391/why-does-intobject10m-throw-specified-cast-is-not-valid-exception) – sarepta Jul 19 '13 at 11:11