Difference between typeof and GetType – Explained
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);
gud one
Anonymous
August 30, 2008 at 9:10 pm
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
look here for a detailed explanation on this
chris
October 20, 2008 at 10:58 pm
This article explains nicely
geeta
October 20, 2008 at 10:59 pm
terrible, terrible article. explains absolutely nothing, and even gets that wrong.
wow
testaaron
October 5, 2011 at 3:57 pm