Maybe I’m crazy, but I thought this was a valid cast:
(new int[]{1,2,3,4,5}).Cast<double>()
Why is LinqPad throwing a
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.
C# allows a conversion from
intdirectly todouble, but not frominttoobjecttodouble.The
Enumerable.Castextension method behaves like the latter. It does not convert values to a different type, it asserts that values are already of the expected type and throws an exception if they aren’t.You could try
(new int[]{1,2,3,4,5}).Select(i => (double)i)instead to get the value-converting behaviour.