Ho difficoltà a capirlo. Si consideri il seguente esempio:C# - Problemi con box/unboxing/intro typecasting. Non capisco
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.
}
L'errore specifico che ottengo in fase di esecuzione è Specified cast is not valid.
Se Controllo immediato (int)thirdTest
in Visual Studio, ottengo un valore Cannot unbox 'thirdTest' as a 'int'
.
Che diavolo sta succedendo qui?
Int16 è in realtà un breve. Quindi penso che puoi sostituire 'Int16 firstTest = Convert.ToInt16 (0);' con 'Int16 firstTest = 0s;' –