Type Testing Operators in C#

Vaibhav • September 11, 2025

As you begin working with different kinds of data in C#, you’ll eventually need to ask questions like: “What type is this value?” or “Can I treat this value as something else?” These questions are answered using type testing operators. In this article, we’ll explore two of them: is and as. These operators help you inspect and safely work with types, even when you’re not sure what kind of data you’re dealing with.

We’ll keep things simple and grounded in what you’ve already learned - variables, data types, and expressions. No classes or inheritance yet. But by the end of this article, you’ll be ready to handle more flexible and dynamic data in your programs.

What Does “Type Testing” Mean?

Type testing means checking whether a value belongs to a specific type. For example, you might want to know if a value is a number, a string, or something else. This is especially useful when working with values that could be one of many types - like user input, or data coming from external sources.

C# is a strongly typed language, which means every value has a type. But sometimes, you’ll work with values that are stored in a more general form, and you’ll need to test their actual type before using them safely.

The is Operator

The is operator checks whether a value is of a specific type. It returns true if the value matches the type, and false otherwise.

string name = "Vaibhav";
bool check = name is string; // true

In this example, the variable name holds a string. The expression name is string checks whether name is a string - and it is, so check becomes true.

You can use is with any type you’ve learned so far: int, double, bool, char, and so on.

int age = 30;
bool isDouble = age is double; // false
bool isInt = age is int;       // true

Here, age is an integer. So age is int is true, but age is double is false.

The is operator doesn’t change the value or convert it. It only checks the type and returns a bool.

The as Operator

The as operator tries to convert a value to a specific type. If the conversion works, you get the value in the new type. If it doesn’t, you get null.

This operator is useful when you want to try a conversion without risking an error. It’s a safe way to attempt a cast.

string text = "Hello";
string result = text as string; // "Hello"

In this case, text is already a string, so text as string gives you the same value.

int number = 42;
// int result = number as int; // ❌ Not allowed

The as operator only works with reference types and nullable value types. You can’t use it with regular value types like int or double directly.

If the conversion fails, as returns null - not an error. This makes it safer than regular casting.

Using as with Nullable Types

You can use as with nullable types like int? or double?. These types can hold either a value or null.

object value = 100;
int? maybeInt = value as int?; // 100

Here, value holds an integer. Since int? is a nullable type, the conversion works and maybeInt becomes 100.

object value = "Not a number";
int? maybeInt = value as int?; // null

In this case, value is a string, so the conversion fails and maybeInt becomes null.

You can combine as with the null-coalescing operator ?? to provide a fallback value. For example: int? result = value as int? ?? 0;

Why Type Testing Matters

Type testing helps you write safer and more flexible code. Instead of assuming a value is of a certain type, you can check or convert it carefully. This prevents errors and makes your programs more reliable.

For example, if you’re working with user input, you might not know whether the input is a number or a string. Using is and as, you can handle each case appropriately.

Common Mistakes to Avoid

Beginners often try to use as with non-nullable types like int. This doesn’t work - use int? instead. Also, don’t forget to check for null after using as. If the conversion fails, you’ll get null, and using it without checking can cause problems.

Real-World Analogy

Imagine you’re sorting items in a box. Some are books, some are clothes, some are tools. Before you use an item, you check what it is. That’s like using is. If you want to treat something as a book, you try - and if it’s not a book, you put it aside. That’s like using as.

Summary

The is and as operators help you work with types safely in C#. Use is to check a value’s type, and as to try converting it. These tools are especially useful when dealing with flexible or unknown data. By using them correctly, you can avoid errors and write more robust programs. As we move into more advanced topics like classes and inheritance, these operators will become even more powerful - but for now, they’re a great way to explore type safety in your code.