This operator is used to check if an object is compatible with a given type. It returns true if caste succeeds and returns false if the caste fails. This operator is ideal to use when we are not interested in resulting variable and just interested in checking the compatibility of an object with a given type.
This operator is more suited to be used in the expressions which requires a boolean context, for example like in if-statement.
static void Main(string args)
int variable1 = 32;
// Checking compatibility of variable1 with three different type parameters using is-operator .
if(variable1 is object)
Console.WriteLine("variable1 is compatible with object type.");
if (variable1 is int)
Console.WriteLine("variable1 is compatible with int type.");
if (variable1 is string)
Console.WriteLine("variable1 is compatible with string type.");
variable1 is compatible with object type.
variable1 is compatible with int type.
Here we see three different types of casts being performed in the if-statement expressions. After examining the out we can see that only first two if-statement bodies have been reached. Their if-expressions are evaluated to true which shows that variable1 is compatible with object and int. The third if-statement body didn't get reached as the corresponding if expression evaluates to false due to the fact that variable1 which is of type int is not compatible with string type.
Note: Condition "variable1 is object" evaluates to true because every type, either directly or indirectly gets inherited from object type and hence its possible to typecast variable1 (of type int) to object type.