System.InvalidCastException Class

public class InvalidCastException : SystemException

Base Types

Object
  Exception
    SystemException
      InvalidCastException

Assembly

mscorlib

Library

BCL

Summary

Represents the error that occurs when an explicit conversion (casting operation) fails because the source type cannot be converted to the destination type.

Description

[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:

]

Example

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()
			

See Also

System Namespace

Members

InvalidCastException Constructors

InvalidCastException() Constructor
InvalidCastException(System.String) Constructor
InvalidCastException(System.String, System.Exception) Constructor


InvalidCastException() Constructor

public InvalidCastException();

Summary

Constructs and initializes a new instance of the InvalidCastException class.

Description

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 .

See Also

System.InvalidCastException Class, System Namespace

InvalidCastException(System.String) Constructor

public InvalidCastException(string message);

Summary

Constructs and initializes a new instance of the InvalidCastException class.

Parameters

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.

Description

This constructor initializes the System.InvalidCastException.Message property of the new instance using message. If message is null , 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 to null .

See Also

System.InvalidCastException Class, System Namespace

InvalidCastException(System.String, System.Exception) Constructor

public InvalidCastException(string message, Exception innerException);

Summary

Constructs and initializes a new instance of the InvalidCastException class.

Parameters

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 .

Description

This constructor initializes the System.InvalidCastException.Message property of the new instance using message, and the System.InvalidCastException.InnerException property using innerException. If message is null , 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.]

See Also

System.InvalidCastException Class, System Namespace