Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C#
C# Reference
C# Keywords
Operator Keywords
 is

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
C# Language Reference
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."
        }
        else
        {
            Console.WriteLine("o is neither Class1 nor Class2.");
        }
    }
    static void Main()
    {
        Class1 c1 = new Class1();
        Class2 c2 = new Class2();
        Test(c1);
        Test(c2);
        Test("a string");
    }
}

Output

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Prefer the as operator in most circumstances      David M. Kean   |   Edit   |   Show History

The example above shows using the is operator to determine if an object is of a type before casting it to that type, ie:

if (o is Class1)
{
   Class1 a = (Class1)o;
 
   // Do something
}

 

For this pattern, it is actually preferred to use the as operator instead:

Class1 a = o as Class1;
 
if (a != null)
{
   // Do something
}

 

Using the as operator and checking for null is cheaper than using the is operator and a cast. However, be aware that this will only work for reference types (classes); you will need to use the first pattern for checking if an object is of a value type (structure).

Flag as ContentBug
Watch out for the order when comparing extended classes      sidonath   |   Edit   |   Show History

If the Class2 extends Class1 it is very easy to make a simple logical error. For example, output of the following snippet is not what one would expect:

class Class1
{
}
class Class2 : Class1
{
}
class IsTest
{
static void Test(object o)
{
if (o is Class1)
{
Console.WriteLine("o is Class1");
}
else if (o is Class2)
{
Console.WriteLine("o is Class2");
}
}
    static void Main()
{
Class1 c1 = new Class1();
Class2 c2 = new Class2();

Test(c1);
Test(c2);
}
}

Output would be:

o is Class1
o is Class1

That is so due to the fact that is operator returns true whenever an object can be cast to the given type. Since c2 can be cast to Class1, output is as stated. To make valid comparison, remember to always first check for derived types and only then base ones. Hence, correct way to check would be:

...
    static void Test(object o)
{
// first check derived types
        if (o is Class2)
{
Console.WriteLine("o is Class1");
}
else if (o is Class1)
{
Console.WriteLine("o is Class2");
}
}
...

Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker