75

I've seen both done in some code I'm maintaining, but don't know the difference. Is there one?

let me add that myCustomer is an instance of Customer

user18931
  • 10,715
  • 9
  • 28
  • 21

7 Answers7

162

The result of both are exactly the same in your case. It will be your custom type that derives from System.Type. The only real difference here is that when you want to obtain the type from an instance of your class, you use GetType. If you don't have an instance, but you know the type name (and just need the actual System.Type to inspect or compare to), you would use typeof.

Important difference

EDIT: Let me add that the call to GetType gets resolved at runtime, while typeof is resolved at compile time.

Community
  • 1
  • 1
Kilhoffer
  • 32,375
  • 22
  • 97
  • 124
  • 39
    I'm voting mostly for the edit. Because that is an important distinction. – Benjamin Autin Sep 26 '08 at 14:07
  • not necessarily exactly the same. Suppose VipCustomer inherits from Customer, then if at some point myCustomer = new VipCustomer(); then its GetType() would be different from type Customer. As you said, runtime and compile time are two very different things. – LongChalk Mar 12 '19 at 10:07
27

GetType() is used to find the actual type of a object reference at run-time. This can be different from the type of the variable that references the object, because of inheritance. typeof() creates a Type literal that is of the exact type specified and is determined at compile-time.

Jeffrey L Whitledge
  • 58,241
  • 9
  • 71
  • 99
19

Yes, there is a difference if you have an inherited type from Customer.

class VipCustomer : Customer
{
  .....
}

static void Main()
{
   Customer c = new VipCustomer();
   c.GetType(); // returns typeof(VipCustomer)
}
Jakub Šturc
  • 35,201
  • 25
  • 90
  • 110
5

For the first, you need an actual instance (ie myCustomer), for the second you don't

Chris Ballard
  • 3,771
  • 4
  • 28
  • 40
5

typeof(foo) is converted into a constant during compiletime. foo.GetType() happens at runtime.

typeof(foo) also converts directly into a constant of its type (ie foo), so doing this would fail:

public class foo
{
}

public class bar : foo
{
}

bar myBar = new bar();

// Would fail, even though bar is a child of foo.
if (myBar.getType == typeof(foo))

// However this Would work
if (myBar is foo)
FlySwat
  • 172,459
  • 74
  • 246
  • 311
2

typeof is executed at compile time while GetType at runtime. That's what is so different about these two methods. That's why when you deal with type hierarchy, you can find out the exact type name of a type simply by running GetType.

public Type WhoAreYou(Base base)
{
   base.GetType();
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
David Pokluda
  • 10,693
  • 5
  • 28
  • 26
1

The typeof operator takes a type as a parameter. It is resolved at compile time. The GetType method is invoked on an object and is resolved at run time. The first is used when you need to use a known Type, the second is to get the type of an object when you don't know what it is.

class BaseClass
{ }

class DerivedClass : BaseClass
{ }

class FinalClass
{
    static void RevealType(BaseClass baseCla)
    {
        Console.WriteLine(typeof(BaseClass));  // compile time
        Console.WriteLine(baseCla.GetType());  // run time
    }

    static void Main(string[] str)
    {
        RevealType(new BaseClass());

        Console.ReadLine();
    }
}
// *********  By Praveen Kumar Srivastava 
Franziee
  • 621
  • 11
  • 28