January 2003
Intermediate to advanced
464 pages
9h 35m
English
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 ...
Read now
Unlock full access