Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

is (C# Reference)

Checks if an object is compatible with a given type. For example, it can be determined if an object is compatible with the string type like this:

if (obj is string)

An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown. For more information, see 7.6.6 Cast expressions.

The is keyword results in a compile-time warning if the expression is known to always be true or to always be false, but typically evaluates type compatibility at run time.

The is operator cannot be overloaded.

Note that the is operator only considers reference conversions, boxing conversions, and unboxing conversions. Other conversions, such as user-defined conversions, are not considered.

// cs_keyword_is.cs
// The is operator.
using System;
class Class1
class Class2

class IsTest
    static void Test(object o)
        Class1 a;
        Class2 b;

        if (o is Class1)
            Console.WriteLine("o is Class1");
            a = (Class1)o;
            // Do something with "a."
        else if (o is Class2)
            Console.WriteLine("o is Class2");
            b = (Class2)o;
            // Do something with "b."
            Console.WriteLine("o is neither Class1 nor Class2.");
    static void Main()
        Class1 c1 = new Class1();
        Class2 c2 = new Class2();
        Test("a string");


o is Class1
o is Class2
o is neither Class1 nor Class2.

For more information, see the following sections in the C# Language Specification:

  • 7.9.9 The is operator

© 2015 Microsoft