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.
if (shape is Circle c) { Console.WriteLine(c.Radius); } Circle? maybe = shape as Circle;
Try it
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.
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
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 Typeistruewhen the run-time type ofxisTypeor derives from it, or implements it, andxis notnull. Anullvalue fails every type test.x is nullandx is not nullare the preferred null checks because they cannot be fooled by an overloaded==operator.- Prefer a pattern like
if (o is Dog d)overvar 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 benull. - Use
aswhennullis 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. asandisonly consider reference, boxing and unboxing conversions. They never apply user-defined implicit or explicit operators.iswith a constant works for numbers, strings, enum members,nullandtrue/false. Relational patterns (> 1) and the combinatorsand,or,notneed C# 9.- Compile-time diagnostics help: if the types can never match (
"x" is int), the compiler warnsCS8121or reports that the expression is alwaysfalse. isagainst a generic type parameter works (value is T) but allocates whenTis a value type andvalueis boxed.