Skip to main content
GetType() and typeof

When to use GetType() and typeof?

Hello Readers,

Both GetType() and typeof are used for checking the type of the object.

When to use GetType() and typeof? Click To Tweet

  • typeof is an operator to obtain a type known at compile-time (or at least a generic type parameter).
  • The operand of typeof is always the name of a type or type parameter – never an expression with a value (e.g. a variable). See the C# language specification for more details.
  • GetType() is a method you call on individual objects, to get the execution-time type of the object.
  • GetType() works at runtime and typeof() is a compile-time operator.
  • typeof is applied to a type. GetType is applied to an object. In both cases, a result is an object of the type System.Type containing meta-information on a type.

If you have,

string s = "hello";

These two lines below are valid.

Type t1 = typeof(string);
Type t2 = s.GetType();

And the comparison below yields true.

t1 == t2 ==> true

GetType() is a method you can call on individual objects, to get the execution-time type of the object. Use GetType() when you want to get the type at execution time. It is a method of the object class that can be used in an instance.

Use typeof when you want to get the type at compilation time. It is an operator to obtain a type known at compile-time (or at least a generic type parameter). The operand for typeof is always the name of a type or type parameter. It can’t be a variable or anything like that. It is calculated at compile time and thus cannot be used for an instance, which is created at runtime.

In both cases, a result is an object of the type System.Type containing meta-information on a type.

Code Example:

using System;
namespace ConsoleApplication1
{
    class Car { }
    class Ford : Car { }
    class Program
    {
        static Car newCar()
        {
            return new Ford();
        }
        static void Main(string[] args)
        {
            Car c = newCar();
            Console.WriteLine(typeof(Car));   //Output : CAR
            Console.WriteLine(c.GetType());   //Output : FORD
            Console.ReadKey();
        }
    }
}

In the above example, within the Main() method, you are dealing with instances of type Car; So, if you care about the declared type, you would use typeof(Car).

However, the newCar() method actually returns an instance of a derived class, Ford, despite declaring the base class as the return type.

If you want to find out about this runtime type, call GetType on the returned instance.

For more on type checking, you can refer this post – Type Checking in C# and VB.NET

What do you think?

Dear Reader,
If you have any questions or suggestions please feel free to email us or put your thoughts as comments below. We would love to hear from you. If you found this post or article useful then please share along with your friends and help them to learn.

Happy Coding!

Dipendra Shekhawat

Hey Reader! I am a full time software developer and part time blogger from Pune, India. I write about technologies like WordPress, ASP.NET, C#, JQuery, HTML, CSS, AJAX, WCF, Web Services. For more you can refer to my blog – http://dipendrashekhawat.com