Why explicit conversion from object to long (Unboxing) is not allowed in C#?

44 views Asked by At

I was reading unboxing and came across this code:

object obj = 22;

long l = (long)obj;

when I ran this code, it throws an exception InvalidCastException

I do not understand why?

For example Microsoft documentation on Boxing and Unboxing shows similar example that works:

int i = 123;
// The following line boxes i.
object o = i;
o = 123;
i = (int)o;  // unboxing
0

There are 0 answers