Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

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

close form
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

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

close form
check circle
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


Comments (0)

Next comments next comments
close comment form