Other Operators

C# has some operators that can't be categorized as easily as the other types. These include the is, as, sizeof(), typeof(), checked(), and unchecked() operators. The following sections explain each operator.

The is Operator

The is operator checks a variable to see if it's a given type. If so, it returns true. Otherwise, it returns false. Here's an example.

int i = 0;
bool isTest = i is int; // isTest = true

The as Operator

The as operator attempts to perform a conversion on a reference type. The following example tries to convert the integer i into a string. If the conversion were successful, the object variable obj would hold a reference to a string object. When the conversion from an as operator fails, it assigns null to the ...

Get C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.