code suivant se comporte étrange (au moins pour moi):Pourquoi cette conversion ne fonctionne-t-elle pas?
int testValue = 1234;
this.ConversionTest(testValue);
private void ConversionTest(object value)
{
long val_1 = (long) (int) value; // works
long val_2 = (long) value; // InvalidCastException
}
Je ne comprends pas pourquoi la distribution directe (explicite) et à long ne fonctionne pas. Quelqu'un peut-il expliquer ce comportement?
Merci
Il s'agit d'une question très fréquemment posée. Mon article sur le sujet est ici: http://blogs.msdn.com/b/ericlippert/archive/2009/03/19/representation-and-identity.aspx –
Notez également qu'il s'agit d'un doublon de nombreux précédents Stack Overflow questions, comme celui-ci: http://stackoverflow.com/questions/1812172 ou celui-ci: http://stackoverflow.com/questions/2651754 ou celui-ci: http://stackoverflow.com/questions/1667169 ou celui-ci: http://stackoverflow.com/questions/1085097 –