IS vs AS Operators in C#

The is and as operators in C# are used for type checking and type conversion. The is operator checks if an object is compatible with a given type and returns a boolean value, while the as operator attempts to convert an object to a specified type and returns null if the conversion fails.

Syntax

Following is the syntax for the is operator −

expr is type

Following is the syntax for the as operator −

expr as type

Where expr is the expression to test and type is the target type.

Using the IS Operator

The is operator performs a runtime type check and returns true if the object is compatible with the specified type, otherwise false

using System;

class One { }
class Two { }

public class Demo {
    public static void Test(object obj) {
        One x;
        Two y;

        if (obj is One) {
            Console.WriteLine("Class One");
            x = (One)obj;
        } else if (obj is Two) {
            Console.WriteLine("Class Two");
            y = (Two)obj;
        } else {
            Console.WriteLine("None of the classes!");
        }
    }

    public static void Main() {
        One o1 = new One();
        Two t1 = new Two();
        Test(o1);
        Test(t1);
        Test("str");
    }
}

The output of the above code is −

Class One
Class Two
None of the classes!

Using the AS Operator

The as operator attempts to convert an object to a specified type. If the conversion fails, it returns null instead of throwing an exception −

using System;

public class Demo {
    public static void Main() {
        object[] obj = new object[2];
        obj[0] = "jack";
        obj[1] = 32;

        for (int i = 0; i < obj.Length; ++i) {
            string s = obj[i] as string;
            Console.Write("{0}: ", i);
            if (s != null)
                Console.WriteLine("'" + s + "'");
            else
                Console.WriteLine("This is not a string!");
        }
    }
}

The output of the above code is −

0: 'jack'
1: This is not a string!

Pattern Matching with IS Operator

Modern C# versions support pattern matching with the is operator, allowing you to check type and assign in one statement −

using System;

public class Demo {
    public static void ProcessObject(object obj) {
        if (obj is string str) {
            Console.WriteLine($"String: {str}");
        } else if (obj is int number) {
            Console.WriteLine($"Integer: {number}");
        } else {
            Console.WriteLine("Unknown type");
        }
    }

    public static void Main() {
        ProcessObject("Hello");
        ProcessObject(42);
        ProcessObject(3.14);
    }
}

The output of the above code is −

String: Hello
Integer: 42
Unknown type

Comparison between IS and AS Operators

IS Operator AS Operator
Returns true or false Returns the converted object or null
Used for type checking only Used for safe type conversion
Cannot convert, only checks compatibility Performs actual conversion if possible
Works with value types and reference types Works only with reference types and nullable types

Conclusion

The is operator is ideal for type checking and pattern matching, while the as operator provides safe type conversion without exceptions. Use is when you need to verify type compatibility, and as when you need to convert objects while handling potential conversion failures gracefully.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements