Why does this:
(new[]{1,2,3}).Cast<decimal>();
result in an
InvalidCastException: Specified cast is not valid.
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.
Yup,
Castdoesn’t do that. Basically it just does reference conversions and unboxing conversions – not conversions between different value types.Use this instead:
Note that pre-.NET 3.5 SP1,
Castdid some more conversions than it does now. I don’t know offhand whether it would have worked then or not, but it definitely doesn’t now.