C#
GetType
typeof
object type
programming differences

What is the difference between myCustomer.GetType and typeofCustomer in C?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In C#, myCustomer.GetType() and typeof(Customer) both produce a Type, but they answer different questions. GetType() asks an object instance what its actual runtime type is. typeof(Customer) asks the compiler for the Type object representing the named type Customer. The difference matters most when inheritance, null references, or generic code are involved.

typeof(Customer) Refers to a Known Type

typeof is a language operator. You use it when the type is known in code.

csharp
1using System;
2
3public class Customer {}
4
5public class Program
6{
7    public static void Main()
8    {
9        Type t = typeof(Customer);
10        Console.WriteLine(t.Name);
11    }
12}

This does not require an instance. It simply returns the Type object for Customer.

That makes typeof useful for:

  • reflection on a known type
  • attribute inspection
  • registration code
  • generic constraints and metadata comparisons

GetType() Uses the Actual Runtime Instance

GetType() is an instance method inherited from System.Object. It tells you the exact runtime type of the object you have.

csharp
1using System;
2
3public class Customer {}
4public class VipCustomer : Customer {}
5
6public class Program
7{
8    public static void Main()
9    {
10        Customer myCustomer = new VipCustomer();
11        Type t = myCustomer.GetType();
12        Console.WriteLine(t.Name);
13    }
14}

This prints VipCustomer, not Customer, because the runtime instance is actually of the derived type.

That is the biggest conceptual difference between the two forms.

Inheritance Makes the Difference Obvious

If there is no inheritance involved, the two results may appear identical and the distinction can feel theoretical. Inheritance makes it concrete.

csharp
1Customer myCustomer = new VipCustomer();
2
3Console.WriteLine(myCustomer.GetType() == typeof(Customer));    // false
4Console.WriteLine(myCustomer.GetType() == typeof(VipCustomer)); // true

So:

  • 'typeof(Customer) means “the type named Customer”'
  • 'myCustomer.GetType() means “the real runtime type of this object”'

Those are not always the same.

GetType() Can Throw on null

Another practical difference is null handling. typeof(Customer) always works because it does not depend on an instance. GetType() requires an actual object reference.

csharp
1Customer myCustomer = null;
2
3// This is safe:
4Console.WriteLine(typeof(Customer).Name);
5
6// This throws NullReferenceException:
7Console.WriteLine(myCustomer.GetType().Name);

If a variable might be null, calling GetType() directly is unsafe unless you guard it.

Use the Right Tool for the Question

Ask yourself which question you are trying to answer:

  • “What is the declared type I want to reference in code?” Use typeof.
  • “What concrete type is this object at runtime?” Use GetType().

For polymorphism checks, you may not want either exact-type comparison. Often the better tool is is or pattern matching:

csharp
1if (myCustomer is VipCustomer vip)
2{
3    Console.WriteLine("VIP logic here");
4}

That expresses assignability and inheritance more clearly than strict type equality.

Reflection and Generics

In generic code, typeof(T) is especially common because it refers to the type parameter itself.

csharp
1public static void PrintTypeName<T>()
2{
3    Console.WriteLine(typeof(T).FullName);
4}

That is very different from calling GetType() on one particular instance, which might be null or might be a subtype of the variable's declared type.

Common Pitfalls

The first pitfall is assuming GetType() and typeof are interchangeable. They are not when inheritance or nullability is involved.

Another issue is using exact type equality when the real intent is “is this object assignable to this type.” In those cases, is is often the better choice.

Developers also forget that GetType() throws if the reference is null, while typeof does not depend on an instance at all.

Finally, comparing runtime type equality can accidentally reject valid derived types when polymorphism was intended.

Summary

  • 'typeof(Customer) returns the Type for the named type Customer.'
  • 'myCustomer.GetType() returns the actual runtime type of the instance.'
  • The difference becomes important with inheritance and null references.
  • Use typeof when the type is known in code and no instance is required.
  • Use GetType() when you need the exact concrete runtime type of an object.

Course illustration
Course illustration

All Rights Reserved.