Casting between interfaces


Summary Casting is used by .NET to jump from one interface to another in the same class. In a Component Object Model (COM), this is a query interface (QI). Visual Basic .NET (VB .NET) and C# cast differently.

In this topic


Casting in VB .NET

The following are the types of cast:
  • Implicit—Does not require additional syntax.
  • Explicit—Requires cast operators.
See the following code example:
[VB.NET]
'Implicit cast.
geometry = point

'Explicit cast.
geometry = CType(point, IGeometry)
geometry = DirectCast(point, IGeometry)
geometry = TryCast(point, IGeometry)
When casting between interfaces, it is acceptable to use implicit casts, because there is no chance of data loss as when casting between numeric types. However, when casts fail, an exception (System.InvalidCastException) is thrown. To avoid handling unnecessary exceptions, it is best to test if the object implements both interfaces beforehand. You can test this with the TypeOf keyword, which is a comparison clause that tests whether an object is derived from or implements a particular type, such as an interface.
The following code example performs an implicit conversion from IPoint to IGeometry only if it is determined at run time that the Point class implements IGeometry:
[VB.NET]
Dim point As New PointClass
Dim geometry As IGeometry
If TypeOf point Is IGeometry Then
    geometry = point
End If
If you prefer using the Option Strict On statement to restrict implicit conversions, use the CType or DirectCast function to make the cast explicit. The following code example adds an explicit cast to the previous code example:
[VB.NET]
Dim point As New PointClass
Dim geometry As IGeometry
If TypeOf point Is IGeometry Then
    geometry = CType(point, IGeometry)
End If
Alternatively, you can skip the TypeOf comparison and use TryCast, which returns Nothing when conversion fails. See the following code example:
[VB.NET]
Dim point As New PointClass
Dim geometry As IGeometry = TryCast(point, IGeometry)
If geometry IsNot Nothing Then
    Console.WriteLine(geometry.GeometryType.ToString())
End If

Casting in C#

In C#, the best method for casting between interfaces is to use the as operator. Using the as operator is a better coding strategy than a straight cast, because it yields a null on a conversion failure rather than raising an exception.
The first line in the following code example is a straight cast. This is an acceptable practice if you are certain the object in question implements both interfaces. If the object does not implement the interface you are attempting to get a handle to, .NET throws an exception. A safer model to use is the as operator, which returns a null if the object cannot return a reference to the desired interface.
[C#]
IGeometry geometry = (IGeometry)point; // Straight cast.
IGeometry geometry = point as IGeometry; // As operator.
The following code example shows how to manage the possibility of a returned null interface handle after an explicit cast:
[C#]
IPoint point = new PointClass();
IGeometry geometry = point as IGeometry;
if (geometry != null)
{
    Console.WriteLine(geometry.GeometryType.ToString());
}
Alternatively, you can test if an object implements a certain interface at run time using the is keyword before performing a straight cast. See the following code example:
[C#]
IPoint point = new PointClass();
if (point is IGeometry)
{
    IGeometry geometry = (IGeometry)point;
    Console.WriteLine(geometry.GeometryType.ToString());
}