This works as expected:
byte b = 7;
var i = (int)b;
While this throws an InvalidCastException:
byte b = 7;
object o = b;
var i = (int)o;
Why does the cast fail from an object when the underlying type is still byte?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because
bytehas an explicit conversion toint, butobjectdoes not.If you tell the compiler the
objectis really abyte, then it will once again allow you to explicitly cast toint.References:
Casting and Type Conversions
byte