I have InvalidCastException when I try to cast 0.0 to double, why is that so? It’s fine when I do (float)value instead.

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.
In general, when you put a value type into an
object(called boxing) you need to unbox it to the exact same value type. You cannot do a conversion to another type instead. This is what happens here.If you really want to convert the object, you first need to unbox it. Say your original value was a
floatbefore you boxed it in anobject:Or use the method proposed by others, which uses
Convert. This has the advantage that the original type doesn’t have to be known.