私はこれを理解するのに苦労しています。次の例を考えてみましょう。
protected void Page_Load(object sender, EventArgs e)
{
// No surprise that this works
Int16 firstTest = Convert.ToInt16(0);
int firstTest2 = (int)firstTest;
// This also works
object secondTest = 0;
int secondTest2 = (int)secondTest;
// But this fails!
object thirdTest = Convert.ToInt16(0);
int thirdtest2 = (int)thirdTest; // It blows up on this line.
}
実行時に発生する特定のエラーは、Visual StudioでSpecified cast is not valid.
QuickWatchを実行すると、値が。になります。(int)thirdTest
Cannot unbox 'thirdTest' as a 'int'
ここで一体何が起こっているのですか?