See Also: InvalidCastException Members
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 CIL instructions throw InvalidCastException:
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()