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);