Manish Pansiniya's Blog

.NET, C#, Javascript, ASP.NET and lots more…:)

Difference between typeof and GetType – Explained

with 5 comments

typeof and GetType produce the exact same information. But the difference is where they get this information from:

  • typeof is used to get the type based on a class. That means if you use typeof with object, it will gives you error. You must pass class as parameter parameter.
  • Where GetType is used to get the type based on an object (an instance of a class). Means GetType needs parameter of object rather than class name.

You can understand more with example.

The following code will output “True”:

  string instance = “”;

  Type type1 = typeof(string);

  Type type2 = instance.GetType();

  Console.WriteLine(type1 == type2);

Written by Manish

September 27, 2007 at 1:32 pm

Posted in .NET, .NET 3.0

5 Responses

Subscribe to comments with RSS.

  1. gud one

    Anonymous

    August 30, 2008 at 9:10 pm

  2. I would say that it is incorrect, or misleading
    to say “typeof is used to get the type based
    on a class”. No, it is based on a type. A class
    is a type but so is a delegate and an interface.
    So the statement is incorrect

    Bob

    October 17, 2008 at 11:46 pm

  3. look here for a detailed explanation on this

    chris

    October 20, 2008 at 10:58 pm

  4. terrible, terrible article. explains absolutely nothing, and even gets that wrong.

    wow

    testaaron

    October 5, 2011 at 3:57 pm


Leave a reply to geeta Cancel reply