This documentation is archived and is not being maintained.

8.4 Reference Conversions

Visual Studio .NET 2003

A value typed as a reference type may always be converted to a base type. Conversions from a base type to a derived type only succeed at run time if the value being converted is a null reference or a reference type that is either the derived type itself or a more derived type.

Within the .NET Framework, it is possible to know at compile time whether a class type implements a particular interface type. Consequently, a class type can always be cast to an interface type that it implements. Similarly, conversions from an interface type to a class type that implements it only succeed at run time if the value being converted is a null reference or a reference type that is either the class type itself or a type derived from the class type.

However, classes that represent COM classes may have interface implementations that are not known until run time. Consequently, a class type may also be converted to an interface type that it does not implement, an interface type may be converted to a class type that does not implement it, and an interface type may be converted to another interface type with which it has no inheritance relationship. In all these cases, a check is performed at run time by the .NET Framework to determine if the class type involved implements the required interface types.

If a reference conversion fails at run time, a System.InvalidCastException exception is thrown.

See Also

8.5 Array Covariance | 8.8 Widening Conversions | 8.9 Narrowing Conversions | 8. Conversions | Conversion Summary (Visual Basic Language Reference) | Implicit and Explicit Conversions (Visual Basic Language Concepts)

Show: