This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Type Conversion with the "As" Operator

Objects can be converted from one type to another, assuming that the types are compatible. Often this is achieved using implicit conversion or explicitly with the cast operator. An alternative to this is the use of the "as" operator.

Type Conversion

When using inheritance hierarchies and interfaces to achieve polymorphism, it is often the case that you will wish to convert an object from its current type into a compatible type. The most popular method for undertaking such conversion is to use either implicit or explicit casting.

Implicit casting occurs when an object is assigned as the value of a variable or method parameter. It happens if the target variable is of a compatible type that is higher in the inheritance hierarchy (a superclass), or is of an interface that is implemented by the object's class, either directly or indirectly. In these cases, no additional work is required of the developer; the conversion happens automatically.

Explicit casting must be used when the conversion is in the opposite direction to that required by implicit casting. For example, when casting an object to one of its subclasses or casting from an interface to a class. To perform explicit casting, the cast operator is used.

In the following example code, we can see a variable being cast from a subclass to its superclass using implicit casting. It is then returned to the subclass using explicit casting. Note the use of the cast operator in the second conversion. The operator is a pair of parentheses () surrounding the target type.

class Program
    static void Main(string[] args)
        Subclass sub = new Subclass();
        Superclass super = sub;             // Implicit cast
        Subclass sub2 = (Subclass)super;    // Explicit cast

class Superclass { }

class Subclass : Superclass { }

Explicit casting is useful but can be problematic when the variable being cast is not compatible with the target type. In this situation an exception is thrown. This can be demonstrated by modifying the contents of the Main method in the previous example as follows:

Superclass super = new Superclass();
Subclass sub = (Subclass)super;             // Exception

The "as" Operator

C# includes another method of performing explicit conversions. Using the "as" operator, an object can be converted from one type to another. Unlike with explicit casting, if the conversion is not possible because the types are incompatible the operation does not throw an exception. Instead, the resultant variable simply contains null.

To demonstrate the use of this operator we will use two examples. First let's try a conversion between two compatible types. This example will be functionally equivalent to the implicit and explicit casting sample described earlier.

Modify the Main method's contents as follows and step through the code to see the results. Note the syntax of the operator. The existing object is placed to the left of the "as" operator and the target type is positioned to the right.

Subclass sub = new Subclass();
Superclass super = sub;
Subclass sub2 = super as Subclass;

We can now try a second conversion but this time using incompatible types. Instead of throwing an exception, the target variable is assigned the null value. This can then be checked for and appropriate action taken.

Superclass super = new Superclass();
Subclass sub = super as Subclass;

if (sub == null)
    Console.WriteLine("Incompatible!");     // Outputs "Incompatible!"

NB: The "as" operator can only be used for converting reference types and for boxing operations. For other conversions, casting must be performed.

9 July 2008