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+

Check Type Compatibility with the "Is" Operator

Polymorphism allows a single variable to contain objects of different types. Sometimes it is necessary to determine whether a variable contains an object that is compatible with a specific type. This can be achieved using the "is" operator.

The "Is" Operator

The "Is" operator is an interesting C# keyword that allows the type of an object to be tested. The operator returns a Boolean value that indicates whether a variable's contents are compatible with a specified class or interface type. This makes "is" useful if you wish to test compatibility before performing a conversion operation, or if your method accepts a base type and you wish to alter functionality according to the actual data type passed in the parameter.

We can demonstrate the use of the "is" operator with a simple example. In this example we will create a base class to define the employees of a company. We will also create two subclasses that represent software developers and administrative staff. An interface will be applied to employee types that are allowed to make purchases on behalf of the company. We will then use the various types with the "is" operator and review the results.

Start by creating a new console application and adding the Employee base class:

abstract class Employee
    private string _name;

    public string Name
        get { return _name; }
        set { _name = value; }

Next we will add the interface for employees that may make purchases. This will contain a single property that determines the value that they have the authority to spend.

interface IPurchaser
    int PurchaseLimit { get; set; }

Finally we can complete the model by creating the two Employee subclasses. In this case, the administrators are permitted to make purchases so the Administrator subclass implements the IPurchaser interface.

Add the Developer class:

class Developer : Employee { }

Finally, add the Administrator class:

class Administrator : Employee, IPurchaser
    private int _purchaseLimit;

    public int PurchaseLimit
        get { return _purchaseLimit; }
        set { _purchaseLimit = value; }

Testing the Operator

We can now use the model to demonstrate the use of the "is" operator. First, let us consider the Developer class. As this inherits from the Employee base class, it is compatible with both Employee and Developer. However, it is not compatible with either the IPurchaser interface or the Administrator class. Add the following code to the Main method of the Program class and executing the program:

Developer bob = new Developer();
compatible = bob is Employee;       // true
compatible = bob is Developer;      // true
compatible = bob is Administrator;  // false
compatible = bob is IPurchaser;     // false

We can also try this with the Administrator class. Objects of this type are compatible with the Employee and Administrator classes and with the IPurchaser interface.

Administrator sue = new Administrator();
compatible = sue is Employee;       // true
compatible = sue is Developer;      // false
compatible = sue is Administrator;  // true
compatible = sue is IPurchaser;     // true

It is important to understand that the compatibility test of the "is" operator is not based upon the declared type of the variable. It is always determined by the underlying type of the object. This can be demonstrated by creating a developer held within an Employee variable.

Employee jim = new Developer();
compatible = jim is Employee;       // true
compatible = jim is Developer;      // true
compatible = jim is Administrator;  // false
compatible = jim is IPurchaser;     // false
29 July 2008