12
This is an extremely frequently asked question. See https://ericlippert.com/2009/03/03/representation-and-identity/ for an explanation.
Snippet:
I get a fair number of questions about the C# cast operator. The most frequent question I get is:
short sss = 123; object ooo = sss; // Box the short. int iii = (int) sss; // Perfectly legal. int jjj = (int) (short) ooo; // Perfectly legal int kkk = (int) ooo; // Invalid cast exception?! Why?Why? Because a boxed
Tcan only be unboxed toT. (*) Once it is unboxed, it’s just a value that can be cast as usual, so the double cast works just fine.(*) Or
Nullable<T>.