The exception that is thrown for invalid casting or explicit conversion.
Assembly: mscorlib (in mscorlib.dll)
Thetype exposes the following members.
|InvalidCastException()||Initializes a new instance of the class.|
|InvalidCastException(String)||Initializes a new instance of the class with a specified error message.|
|InvalidCastException(SerializationInfo, StreamingContext)||Initializes a new instance of the class with serialized data.|
|InvalidCastException(String, Exception)||Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception.|
|InvalidCastException(String, Int32)||Initializes a new instance of the class with a specified message and error code.|
|Data||Gets a collection of key/value pairs that provide additional user-defined information about the exception. (Inherited from Exception.)|
|HelpLink||Gets or sets a link to the help file associated with this exception. (Inherited from Exception.)|
|HResult||Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (Inherited from Exception.)|
|InnerException||Gets the Exception instance that caused the current exception. (Inherited from Exception.)|
|Message||Gets a message that describes the current exception. (Inherited from Exception.)|
|Source||Gets or sets the name of the application or the object that causes the error. (Inherited from Exception.)|
|StackTrace||Gets a string representation of the immediate frames on the call stack. (Inherited from Exception.)|
|TargetSite||Gets the method that throws the current exception. (Inherited from Exception.)|
|Equals(Object)||Determines whether the specified object is equal to the current object. (Inherited from Object.)|
|Finalize||Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)|
|GetBaseException||When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. (Inherited from Exception.)|
|GetHashCode||Serves as the default hash function. (Inherited from Object.)|
|GetObjectData||When overridden in a derived class, sets the SerializationInfo with information about the exception. (Inherited from Exception.)|
|GetType||Gets the runtime type of the current instance. (Inherited from Exception.)|
In XNA Framework 3.0, this member is inherited from Object.GetType().
In Portable Class Library Portable Class Library, this member is inherited from Object.GetType().
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|ToString||Creates and returns a string representation of the current exception. (Inherited from Exception.)|
An 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 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 Boolean value to a Char or a DateTime value to an Int32 throws an exception. The following example calls both the Boolean.IConvertible.ToChar and Convert.ToChar(Boolean) methods to convert a Boolean value to a Char. In both cases, the method call throws an exception.
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.
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 a null reference (Nothing in Visual Basic).
For an explicit reference conversion to be successful, the source value must be a null reference (Nothing in Visual Basic), 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 exception:
uses the HRESULT COR_E_INVALIDCAST, which has the value 0x80004002.
For a list of initial property values for an instance of , see the InvalidCastException constructors.
.NET FrameworkSupported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
XNA FrameworkSupported in: 3.0, 2.0, 1.0
Portable Class LibrarySupported in: Portable Class Library
Supported in: Windows Phone 8.1
Supported in: Windows Phone Silverlight 8.1
Supported in: Windows Phone Silverlight 8