C# is Operator

Beginner
5 min read
Updated: Nov 2026
3 examples
Operator

Definition

In C#, the is operator checks whether a value matches a type or a pattern. It returns true or false.

It also lets you create a new variable in the same step. This is called pattern matching, and it replaces many old cast-and-check blocks.

Syntax

Write the value, then is, then the type or pattern.

C#
object value = "hello";

if (value is string text) {
    Console.WriteLine(text.Length);
}
CallWhat it does
value is stringOnly checks the type.
value is string textChecks the type and creates text.
value is not nullChecks that the value exists.

Need the exact type at run time? See typeof and GetType().

Common Patterns

PatternDescription
x is stringTrue when x is a string.
x is string sType check, and also stores the value in s.
x is nullTrue when x is null.
x is not nullTrue when x is not null.
n is >= 18 and < 65Relational patterns joined with and / or.

C# Examples

Example 1 — Basic Type Check

Ask whether an object holds a string or an int.

C#
using System;

class Program {
    static void Main() {
        object value = "hello";

        Console.WriteLine(value is string);
        Console.WriteLine(value is int);
    }
}

How It Works

  1. value is string. The object really holds a string, so the result is True.
  2. value is int. It does not hold an int, so the result is False.
  3. No exception. A failed test just returns false.

Example 2 — Check and Use in One Step

Create a typed variable while testing the type.

C#
using System;

class Program {
    static void Main() {
        object[] items = { 42, "text", null, 3.5 };

        foreach (object item in items) {
            if (item is null) {
                Console.WriteLine("null");
            } else if (item is int n) {
                Console.WriteLine($"int {n}");
            } else if (item is string s) {
                Console.WriteLine($"string of length {s.Length}");
            } else {
                Console.WriteLine($"other: {item}");
            }
        }
    }
}

How It Works

  1. is int n. If the test passes, n is ready to use as an int.
  2. is string s. Same idea, with s as a string.
  3. No casting. You do not need (int)item anywhere.

Example 3 — null and Relational Patterns

Use is not null and range patterns for short, readable checks.

C#
using System;

class Program {
    static void Main() {
        string name = "Sam";
        int age = 25;

        Console.WriteLine(name is not null);
        Console.WriteLine(age is >= 18 and < 65);
        Console.WriteLine(age is < 0 or > 120);
    }
}

How It Works

  1. is not null. A clear way to say "this value exists."
  2. and. Both conditions must be true. 25 is at least 18 and less than 65.
  3. or. Either condition is enough. 25 is not below 0 and not above 120, so the result is False.

Frequently Asked Questions

It checks whether a value matches a type or pattern and returns true or false.
is returns a bool. as returns the converted object, or null when the conversion fails.
It returns false for a type check, because null has no type. Use is null to test for null.
It is value is string text. The type is checked and the new variable text is created in one step.
Yes. Use and, or, and not, for example age is >= 18 and < 65.

Next: String Methods

Browse every C# string method with simple examples.

String Methods index →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful