What is the difference between the typeof() keyword and the GetType() method in C#

A catchy question during an interview...

What is the purpose of having two actions in c# that seem to do the same? On one side we have the typeof() keyword and on the other side the GetType() method.

The answer is quite simple. The typeof() keyword works with classes while the GetType() method works with object instances. Take a look at the following code:

static void Main(string[] args)
    var typeOne = typeof(String);
    var typeTwo = "123".GetType();

As you can see you can only use the typeof keyword when dealing with Classes. String is the name of the class but not an instance of an object by itself.

In the other hand, once we have an object instance and we wonder about its type, we can get it throught its method GetType(). All Objects contain the GetType() method.

FINALLY, GetType is a method of the instance object, not a static method of the class. FOR EXAMPLE, the following code is wrong and won't compile:

var typeThree = String.GetType();