Last post Sep 24, 2016 05:08 AM by Yohann Lu
Sep 24, 2016 03:32 AM|vinodkpasi|LINK
Why following program is throwing InvalidCastException?
string s = "12";
object o = s;
int i = (int)o;//InvalidCastException
int i = 12;
object o = i;
long j = (long)o;//InvalidCastException
Sep 24, 2016 05:08 AM|Yohann Lu|LINK
You can find the following error message.
Additional information: Specified cast is not valid.
It is caused by the limitations of an unboxing cast. You can use Console.WriteLine(Convert.ToInt32(o)) rather than (int)o. It takes an object and supports all of the types. You can refer the following link.
Representation and Identity: