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.
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.
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With