我很难理解这一点。考虑以下示例:
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'
这到底是怎么回事?