Skip to content

is and as operators

Context: is checks if an object is compatible with a given type; as performs a safe cast (returns null if incompatible).

object obj = "Hello";
if (obj is string)
{
Console.WriteLine("It's a string");
}
// Pattern matching (C# 7+)
if (obj is string s)
{
Console.WriteLine($"Length: {s.Length}");
}
object obj = "Hello";
string str = obj as string;
if (str != null)
{
Console.WriteLine(str.Length);
}
// If obj is not a string, str is null (no exception)
  • (string)obj throws InvalidCastException on failure.
  • as returns null (only for reference types and nullable value types).
  • is does not cast; just checks.
if (shape is Circle circle)
{
Console.WriteLine($"Radius: {circle.Radius}");
}