Having started programming in c/c++ i've always been used to casting between types using () operator:
int i = (int)x
With the coming of .Net framework ValueTypes and ReferenceTypes were introduced and we started hearing about boxing and unboxing.
if you're not familiar with these terms pick a link from this search and read up.
Also we've been introduced to System.Convert class and IConvertible interface.
But old habits are hard to break and i've been preffering the (int) to Convert.ToInt32.
For all thing considered i've been treating them as equals. But i've been reminded that they're not with a nice little
error message saying Specified cast is not valid.
This is the shortened code i was running:
object o = (Int16)55;
int convert1 = Convert.ToInt32(o); // ok
int convert2 = (int)o; // error
So why does this happen?
Well let's take it step by step.
1. 55 is Int32 by default as this line proves: 55.GetType().ToString()
2. By doing (Int16)55 we downcast the Int32 to Int16 since it's less than 32767 which is the max Int16 value
no boxing yet since both Int32 and Int16 are ValueTypes
3. object o = (Int16)55; boxes the Int16 into a System.Object thus making it a reference type
The thing about boxing and unboxing is that we have to know what type are we boxing and we must unbox it to the same type.
And when we have to explicitly unbox objects to their types. That's why we can't do simple
int convert2 = o;
And the difference between the two is:
int convert2 = (int)o;
tries to directly cast the object to Int32 but since this violates the rule that unboxing must be done to the same type
which is Int16 in this case an above error is returned.
int convert1 = Convert.ToInt32(o);
uses IConvertible interface to cast it. So it internaly unboxes the object o to Int16 and then directly casts the Int16 to Int32.
Isn't boxing and unboxing real "fun"?
But it's nice of the .Net framework to remind us of such things every now an then... :)