About This eBook by Bart De Smet

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

The is Operator

Casts, as described previously, should be used only if you’re absolutely sure that the object can be converted to the specified type. If it can’t, a runtime exception occurs. In effect, think about this exception as the absolute latest time where a typing error can be caught: Face it, you have a bug.

So, what if we’re just interested in knowing whether an object has a particular runtime type? In other words, we want to make a runtime type check, essentially resulting in a Boolean truth value. This is what the is operator is all about:

object o = "Bart";bool isString = o is string; // truebool isInt    = o is int;    // false

The is operator is concerned only with types and considers type hierarchies as part of its operation. For ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required