Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invalid cast exception on int to double

Tags:

People also ask

What is Invalid cast exception?

An InvalidCastException exception is thrown when the conversion of an instance of one type to another type is not supported. For example, attempting to convert a Char value to a DateTime value throws an InvalidCastException exception.

Which of the following operator does not throw an exception if the cast fails?

The as operator never throws an exception, and if the conversion is not possible, it will return null. Example: In this example, we only have one cast on each block of code, and also this approach allows you to avoid any exceptions that may occur at execution time.


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.

?