C# check type
last modified November 6, 2026
In this article we show how to check types in C# with typeof,
is, and as operators, and the GetType
method.
In C#, every variable and expression has a type. A type is a set of values and the allowable operations on those values.
A type stores the following information:
- storage space that a variable of the type requires
- maximum and minimum values of the type
- type members such as fields or methods
- base type from which it inherits
- implemented interfaces
- allowable operations
The compiler uses this information to ensure type-safe operations.
The System.Type class represents type declarations.
We can check type with the typeof operator, the is
operator, or the GetType method. The typeof operator
obtains the System.Type instance for a type. The type is known at
compile time; the operator works on type names, not on variables.
The GetType method gets the type of the current object instance.
It returns the actual runtime type, which may be more specific than the
declared type of the variable.
The is operator checks if an instance is compatible with a type,
that is, whether the type is in the instance's inheritance tree or among its
implemented interfaces. In addition, it allows type-safe casting.
C# typeof example
The first example uses the typeof operator to print the
System.Type for built-in and custom types.
Type t = typeof(int); Console.WriteLine(t); Console.WriteLine(typeof(List<int>)); Console.WriteLine(typeof(string)); Console.WriteLine(typeof(double)); Console.WriteLine(typeof(float)); Console.WriteLine(typeof(decimal)); Console.WriteLine(typeof(User)); Console.WriteLine(typeof(List<int>).Name); Console.WriteLine(typeof(int).IsValueType); Console.WriteLine(typeof(User).IsClass); record User(string Name, string Occupation);
The example prints System.Type for int, List<int>, string,
double, float, decimal and User.
Console.WriteLine(typeof(List<int>).Name); Console.WriteLine(typeof(int).IsValueType); Console.WriteLine(typeof(User).IsClass);
The Type object has many properties describing the type. The
Name property returns the short name; for generic types, the
number after the backtick is the number of type parameters. The
IsValueType and IsClass properties tell us the kind
of the type.
$ dotnet run System.Int32 System.Collections.Generic.List`1[System.Int32] System.String System.Double System.Single System.Decimal User List`1 True True
C# GetType example
The following example checks the types of several variables.
int x = 34;
string word = "falcon";
decimal i = 23.54m;
if (x.GetType() == typeof(int))
{
Console.WriteLine("x has int type");
}
if (word.GetType() == typeof(string))
{
Console.WriteLine("word has string type");
}
if (i.GetType() == typeof(decimal))
{
Console.WriteLine("i has decimal type");
}
object o = new List<string>();
Console.WriteLine(o.GetType());
Console.WriteLine(o.GetType().Name);
We define an integer, string, and decimal variables. We use the
GetType method to check the types at runtime.
object o = new List<string>(); Console.WriteLine(o.GetType());
The variable is declared as object, but GetType
returns the actual type of the instance.
$ dotnet run x has int type word has string type i has decimal type System.Collections.Generic.List`1[System.String] List`1
C# is operator
The is operator checks if an object is compatible with a given
type, i.e. it is in its inheritance tree.
Base _base = new();
Derived derived = new();
Console.WriteLine(_base is Base);
Console.WriteLine(_base is object);
Console.WriteLine(derived is Base);
Console.WriteLine(_base is Derived);
class Base { }
class Derived : Base { }
We create two objects from user defined types.
class Base { }
class Derived : Base { }
We have a Base and a Derived class. The
Derived class inherits from the Base class.
Console.WriteLine(_base is Base); Console.WriteLine(_base is object);
Base equals Base and so the first line prints True.
The Base is also compatible with Object type. This is
because each class inherits from the mother of all classes — the
Object class.
Console.WriteLine(derived is Base); Console.WriteLine(_base is Derived);
The derived object is compatible with the Base class because it
explicitly inherits from the Base class. On the other hand, the
_base object has nothing to do with the Derived class.
$ dotnet run True True True False
C# exact type vs compatible type
The is operator and the comparison of GetType with
typeof answer different questions. The first checks
compatibility, the second the exact type.
Base obj = new Derived();
Console.WriteLine(obj is Base);
Console.WriteLine(obj.GetType() == typeof(Base));
Console.WriteLine(obj.GetType() == typeof(Derived));
Console.WriteLine(typeof(Base).IsAssignableFrom(obj.GetType()));
Console.WriteLine(typeof(Derived).IsAssignableTo(typeof(Base)));
Console.WriteLine(typeof(List<int>).IsAssignableTo(typeof(IEnumerable<int>)));
class Base { }
class Derived : Base { }
A Derived instance is stored in a variable of type
Base.
Console.WriteLine(obj is Base); Console.WriteLine(obj.GetType() == typeof(Base)); Console.WriteLine(obj.GetType() == typeof(Derived));
The object is compatible with Base, but its exact type is
Derived.
Console.WriteLine(typeof(Base).IsAssignableFrom(obj.GetType())); Console.WriteLine(typeof(Derived).IsAssignableTo(typeof(Base))); Console.WriteLine(typeof(List<int>).IsAssignableTo(typeof(IEnumerable<int>)));
When we have only Type objects, for instance in reflection code,
we check compatibility with the IsAssignableFrom and
IsAssignableTo methods. They also work for interfaces.
$ dotnet run True False True True True True
C# type-safe checking with is
We can perform type-safe casting with the is operator.
object[] vals = [
12, "falcon", 3, 1, true, 20
];
foreach (var e in vals)
{
if (e is int val)
{
Console.WriteLine($"{val} powered is {val * val}");
}
}
We have an array of objects. For all integers, we calculate their squares.
if (e is int val)
{
Console.WriteLine($"{val} powered is {val * val}");
}
If the conversion is possible, the is operator creates a local
variable val of int type. This is called a
declaration pattern.
$ dotnet run 12 powered is 144 3 powered is 9 1 powered is 1 20 powered is 400
C# OfType
Alternatively, we can use the LINQ's OfType method for the job.
object[] vals = [
12, "falcon", 3, 1, true, 20
];
var res = vals.OfType<int>();
foreach (var e in res)
{
Console.WriteLine($"{e} powered is {e * e}");
}
The OfType method filters the elements of an
IEnumerable based on a specified type. The result is a sequence
of integers, so no casting is needed inside the loop.
$ dotnet run 12 powered is 144 3 powered is 9 1 powered is 1 20 powered is 400
C# as operator
The as operator converts a value to the given reference or
nullable type. Unlike a cast, it does not throw an exception; if the
conversion is not possible, it returns null.
object?[] vals = ["falcon", 12, "sky", null];
foreach (var e in vals)
{
string? s = e as string;
if (s is not null)
{
Console.WriteLine($"{s} has {s.Length} characters");
}
else
{
Console.WriteLine($"'{e}' is not a string");
}
}
object o = 12;
try
{
var s = (string)o;
}
catch (InvalidCastException ex)
{
Console.WriteLine(ex.Message);
}
We try to convert the elements of an array to strings.
string? s = e as string;
For the integer and the null element, the as operator returns
null. Today, the is operator with a declaration
pattern is usually preferred.
var s = (string)o;
A cast expression throws an InvalidCastException if the
conversion fails.
$ dotnet run falcon has 6 characters '12' is not a string sky has 3 characters '' is not a string Unable to cast object of type 'System.Int32' to type 'System.String'.
C# check boxed value
With the is operator, we can check the actual type of a boxed
value. Boxing is the process of converting a value type to the type
object.
object o = 12; Console.WriteLine(o is int); Console.WriteLine(o is double); Console.WriteLine(o is long); object o2 = "falcon"; Console.WriteLine(o2 is string); Console.WriteLine(o2 is char); int? n = 5; object o3 = n; Console.WriteLine(o3.GetType()); Console.WriteLine(o3 is int?);
The example determines the actual type of boxed values. A boxed
int is not a long, even though an
int converts implicitly to long; the is
operator does not consider numeric conversions.
int? n = 5; object o3 = n;
When a nullable value type with a value is boxed, the underlying value is
boxed. Therefore, GetType returns System.Int32.
$ dotnet run True False False True False System.Int32 True
C# nullable types and null checks
The Nullable.GetUnderlyingType method returns the underlying type
of a nullable value type, or null if the type is not nullable.
Type t1 = typeof(int?); Type t2 = typeof(int); Console.WriteLine(t1); Console.WriteLine(Nullable.GetUnderlyingType(t1)); Console.WriteLine(Nullable.GetUnderlyingType(t2) is null); object? val = null; Console.WriteLine(val is null); Console.WriteLine(val is object); Console.WriteLine(val is not null);
We examine the int? type and check a null reference.
Console.WriteLine(val is null); Console.WriteLine(val is object); Console.WriteLine(val is not null);
A null value is not of any type, so the is object
check returns false. The is null and is not null
patterns are the recommended way of checking for null, because they cannot be
affected by an overloaded == operator.
$ dotnet run System.Nullable`1[System.Int32] System.Int32 True True False False
C# check type in generic code
In generic methods, typeof(T) returns the type argument.
Describe(12);
Describe("falcon");
Describe(3.5);
Describe(new List<int> { 1, 2 });
void Describe<T>(T val)
{
Console.WriteLine($"{typeof(T).Name}: {val}");
if (typeof(T) == typeof(int))
{
Console.WriteLine(" an integer");
}
if (val is IEnumerable<int> seq)
{
Console.WriteLine($" a sequence with sum {seq.Sum()}");
}
}
The Describe method prints the name of the type argument and
performs additional checks.
if (typeof(T) == typeof(int))
The comparison checks the type argument itself. For value types, the JIT compiler creates a specialized version of the method and removes the check entirely in the versions where it is false.
if (val is IEnumerable<int> seq)
The is operator works with generic values as well.
$ dotnet run Int32: 12 an integer String: falcon Double: 3.5 List`1: System.Collections.Generic.List`1[System.Int32] a sequence with sum 3
C# switch expression type pattern
Types can be patterns of the switch expression.
int age = 23;
string name = "Peter";
List<string> colors = ["blue", "khaki", "orange"];
int[] nums = [1, 2, 3, 4, 5];
Console.WriteLine(Check(age));
Console.WriteLine(Check(name));
Console.WriteLine(Check(colors));
Console.WriteLine(Check(nums));
Console.WriteLine(Check(3.4f));
string Check(object val) => val switch
{
int => "integer",
string => "string",
List<string> => "list of strings",
Array => "array",
_ => "unknown"
};
In the example, we check the types of variables using switch expression. The
arms are tried from top to bottom; the float value does not
match any type pattern, so the discard arm is used.
$ dotnet run integer string list of strings array unknown
Source
Type-testing operators and cast expressions
In this article we showed how to check types in C# with typeof,
is, as, and GetType.
Author
List all C# tutorials.