See Also: InvalidCastException Members
An InvalidCastException exception is thrown when the conversion of an instance of one type to another type is not supported. It differs from an OverflowException exception, which is thrown when a conversion of one type to another is supported, but the value of the source type is outside the range of the target type.
For information about conversions supported by the system, see the Convert class. For errors that occur when the destination type can store source type values, but is not large enough to store a specific source value, see the OverflowException exception.
In many cases, your language compiler detects that no conversion exists between the source type and the target type and issues a compiler error.
Some of the conditions under which an attempted conversion throws an InvalidCastException exception include the following:
You directly or indirectly call a primitive type's IConvertible implementation that does not support a particular conversion. For example, trying to convert a bool value to a char or a DateTime value to an int throws an InvalidCastException exception. The following example calls both the bool.System#IConvertible#ToChar(IFormatProvider) and Convert.ToChar(bool) methods to convert a bool value to a char. In both cases, the method call throws an InvalidCastException exception.
code reference: System.InvalidCastException#2
You've called the Convert.ChangeType(object, Type) method to convert an object from one type to another, but one or both types don't implement the IConvertible interface.
Neither the source type nor the target type defines an explicit or narrowing conversion between the two types, and the IConvertible implementation of one or both types doesn't support a conversion from the source type to the target type.
You are downcasting, that is, trying to convert an instance of a base type to one of its derived types. In the following example, trying to convert a Person object to a PersonWithID object fails.
code reference: System.InvalidCastException#1
As the example shows, the downcast succeeds only if the Person object was created by an upcast from a PersonWithId object to a Person object, or if the Person object is null.
For an explicit reference conversion to be successful, the source value must be null, or the object type referenced by the source argument must be convertible to the destination type by an implicit reference conversion.
The following intermediate language (IL) instructions throw an InvalidCastException exception:
castclass
refanyval
unbox
InvalidCastException uses the HRESULT COR_E_INVALIDCAST, which has the value 0x80004002.
For a list of initial property values for an instance of InvalidCastException, see the InvalidCastException.#ctor constructors.
The following example demonstrates an error that causes a InvalidCastException exception.
C# Example
using System; public class InvalidCastExample { public static void Main() { object obj = new Object(); int i; try { i = (int) obj; } catch( InvalidCastException e ) { Console.WriteLine("Caught: {0}", e); } } }
The output is
Example
Caught: System.InvalidCastException: Specified cast is not valid. at InvalidCastExample.Main()