O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

10.6. The is operator (Java's instanceof operator)

C#'s is operator is used to determine if the runtime type of an object is equivalent to another, much like how Java's instanceof is used.

It goes like this:

<expression> is <type>

The is operator returns a boolean result (true or false).

The is operator returns true if:

  • <expression> is not null AND

  • <expression> is an object which is either a subclass of <type> or is a class that implemented <type> (in this case, <type> is an interface) – in other words, if <expression> can be successfully cast into <type>.

Let's study an example. The output is shown as comments. For convenience, the inheritance relations between the classes in this example are shown in Figure 10.2.

 1: using System; 2: 3: interface ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required