To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (an extended version)
* By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

Free PVS-Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
Type check in C#: typeof, GetType, is

Type check in C#: typeof, GetType, is

Sep 30 2021

One of the basic and most common operations with types is their check at runtime. In different cases — to get information about the type and to check the type — we should use different methods and operators. Below is their brief overview.

The typeof operator

The typeof operator returns the System.Type instance. The instance corresponds to the type, the name of which is specified in the argument.

Type typeOfInt = typeof(int);
// [System.Int32]

Type typeOfString = typeof(string);
// [System.String]

Note that in the case of generic types, the typeof operator can accept both arguments of a constructed and an unbound type.

var typeOfGenericList = typeof(List<>);
// [System.Collections.Generic.List`1[T]]

var typeOfListOfStrings = typeof(List<String>);
// [System.Collections.Generic.List`1[System.String]]

The GetType method

GetType is an instance method of the Object class. This method can obtain the actual type of an object at the application runtime.

Object obj = new Object();
Object str = String.Empty;

Type type1 = obj.GetType();
// [System.Object]

Type type2 = str.GetType();
// [System.String]

The is operator

At runtime, the is operator checks whether the expression type is compatible with the type specified in the operand.

Object obj = new Object();
Object str = String.Empty;

Console.WriteLine(obj is Object); // True
Console.WriteLine(obj is String); // False

Console.WriteLine(str is Object); // True
Console.WriteLine(str is String); // True

This example shows that the is operator doesn't check the exact match. Instead, the operator checks the compatibility, meaning that the actual object type is specified or derived from it.

The is operator also has a variety of other features. They are listed below.

Check for null

After we check the actual type, we also check the value for the null inequation.

Object obj = null;
Console.WriteLine(obj is Object); // False, since obj is null

The boxed type check

We can check the boxed value actual type with the is operator.

Object obj = 42; // boxing

Console.WriteLine(obj is int); // True
Console.WriteLine(obj is double); // False

The Nullable<T> underlying type check

The is operator allows you to check whether a value exists in the Nullable<T> instance (Nullable<T>.HasValue) and its type.

int? nullableInt1 = 62;
int? nullableInt2 = null;

Console.WriteLine(nullableInt1 is int?);   // True
Console.WriteLine(nullableInt1 is int);    // True
Console.WriteLine(nullableInt1 is double); // False

Console.WriteLine(nullableInt2 is int?);   // False
Console.WriteLine(nullableInt2 is int);    // False
Console.WriteLine(nullableInt2 is double); // False

How to choose between typeof, GetType, is

To select an operator/method for working with types, you can use the following approach:

  • use the typeof operator to obtain the System.Type instance for the type name;
  • if you need to check the exact type match, use the System.Type instance obtained via the GetType method;
  • if the compatibility check is sufficient, use the is operator.

The example below clearly demonstrates the difference between type checking via the GetType method and via the is operator.

class A     { .... }        
class B : A { .... }
class C : B { .... }

void Foo()
{
  A obj = new C();

  Console.WriteLine(obj.GetType() == typeof(B)); // False
  Console.WriteLine(obj is B);                   // True
}

The actual type of the object referenced by obj is C. Therefore, the GetType method for obj returns an instance of System.Type corresponding to C. The result of the typeof(B) operator is an instance describing type B. Comparing objects describing different types, as expected, results in false.

The is operator checks for compatibility, not exact match. Since the compatibility of an object of type C with B is checked, the result will be true.

Popular related articles
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: Nov 21 2018

Author: Andrey Karpov

A brief description of technologies used in the PVS-Studio tool, which let us effectively detect a large number of error patterns and potential vulnerabilities. The article describes the implementati…
PVS-Studio for Java

Date: Jan 17 2019

Author: Andrey Karpov

In the seventh version of the PVS-Studio static analyzer, we added support of the Java language. It's time for a brief story of how we've started making support of the Java language, how far we've co…
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: Jul 31 2017

Author: Andrey Karpov

After I wrote quite a big article about the analysis of the Tizen OS code, I received a large number of questions concerning the percentage of false positives and the density of errors (how many erro…
Static analysis as part of the development process in Unreal Engine

Date: Jun 27 2017

Author: Andrey Karpov

Unreal Engine continues to develop as new code is added and previously written code is changed. What is the inevitable consequence of ongoing development in a project? The emergence of new bugs in th…
The Last Line Effect

Date: May 31 2014

Author: Andrey Karpov

I have studied many errors caused by the use of the Copy-Paste method, and can assure you that programmers most often tend to make mistakes in the last fragment of a homogeneous code block. I have ne…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: Oct 22 2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…
PVS-Studio ROI

Date: Jan 30 2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…
Appreciate Static Code Analysis!

Date: Oct 16 2017

Author: Andrey Karpov

I am really astonished by the capabilities of static code analysis even though I am one of the developers of PVS-Studio analyzer myself. The tool surprised me the other day as it turned out to be sma…
The way static analyzers fight against false positives, and why they do it

Date: Mar 20 2017

Author: Andrey Karpov

In my previous article I wrote that I don't like the approach of evaluating the efficiency of static analyzers with the help of synthetic tests. In that article, I give the example of a code fragment…
The Ultimate Question of Programming, Refactoring, and Everything

Date: Apr 14 2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…

Comments (0)

Next comments
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept