Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The is and as operators

Reference for the C# is and as operators: test a value's type or pattern, convert safely without exceptions, and compare them with a direct cast.

is asks a question about a value: "is this object of that type?" or, more generally, "does it match this pattern?". It returns a bool, and with a declaration pattern it can also give you the value already converted to the new type. as tries to convert a reference (or nullable) value to a type and returns null if that is not possible instead of throwing.

Together with the cast (T)x, these are the three ways to move from a general type like object to a specific one. They differ in how they report failure.

C#
if (shape is Circle c)
{
    Console.WriteLine(c.Radius);
}
Circle? maybe = shape as Circle;

Try it

C# Type tests with is

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# as versus a direct cast

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
bool   expr is Type
bool   expr is Type name       // declaration pattern, name is assigned when true
bool   expr is pattern         // constants, relational, not/and/or, property...
T?     expr as Type
Operand Description
expr Any expression. For as, the result type must be a reference type or nullable value type.
Type The target type. For as, it cannot be a non-nullable value type (x as int is error CS0077).
name New variable holding the converted value. It is definitely assigned only where the test succeeded.

Returns: is returns true or false. as returns the converted reference or null.

Errors: a direct cast (T)x throws InvalidCastException when the conversion fails. is and as never throw for a failed type test.

Usage notes

  • x is Type is true when the run-time type of x is Type or derives from it, or implements it, and x is not null. A null value fails every type test.
  • x is null and x is not null are the preferred null checks because they cannot be fooled by an overloaded == operator.
  • Prefer a pattern like if (o is Dog d) over var d = o as Dog; if (d != null). It does the test and the conversion in one step, and the variable is out of reach where it would be null.
  • Use as when null is a normal outcome and you will go on with ?. or ??. Use a cast when a wrong type means a bug and an exception is the right response.
  • as and is only consider reference, boxing and unboxing conversions. They never apply user-defined implicit or explicit operators.
  • is with a constant works for numbers, strings, enum members, null and true/false. Relational patterns (> 1) and the combinators and, or, not need C# 9.
  • Compile-time diagnostics help: if the types can never match ("x" is int), the compiler warns CS8121 or reports that the expression is always false.
  • is against a generic type parameter works (value is T) but allocates when T is a value type and value is boxed.