public class InvalidCastException : SystemException
Object
Exception
SystemException
InvalidCastException
mscorlib
BCL
Represents the error that occurs when an explicit conversion (casting operation) fails because the source type cannot be converted to the destination type.
[Note: For information on 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 OverflowException exception.
The following IL instructions throw InvalidCastException:
]
- castclass
- refanyval
- unbox
The following example demonstrates an error that causes a InvalidCastException exception.
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
Caught: System.InvalidCastException: Specified cast is not valid. at InvalidCastExample.Main()
System Namespace
InvalidCastException Constructors
InvalidCastException() Constructor
InvalidCastException(System.String) Constructor
InvalidCastException(System.String, System.Exception) Constructor
public InvalidCastException();
Constructs and initializes a new instance of the InvalidCastException class.
This constructor initializes the System.InvalidCastException.Message property of the new instance to a system-supplied message that describes the error, such as "Cannot cast from source type to destination type." This message takes into account the current system culture.The System.InvalidCastException.InnerException property is initialized to
null
.
System.InvalidCastException Class, System Namespace
public InvalidCastException(string message);
Constructs and initializes a new instance of the InvalidCastException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
This constructor initializes the System.InvalidCastException.Message property of the new instance using message. If message isnull
, the System.InvalidCastException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments. The System.InvalidCastException.InnerException property is initialized tonull
.
System.InvalidCastException Class, System Namespace
public InvalidCastException(string message, Exception innerException);
Constructs and initializes a new instance of the InvalidCastException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
- innerException
- An instance of Exception that is the cause of the current exception. If innerException is non-null, then the current exception was raised in a catch block handling innerException .
This constructor initializes the System.InvalidCastException.Message property of the new instance using message, and the System.InvalidCastException.InnerException property using innerException. If message isnull
, the System.InvalidCastException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.[Note: For information on inner exceptions, see System.Exception.InnerException.]
System.InvalidCastException Class, System Namespace