GetType and TypeOf confusion

Both VB and C# have an operator called TypeOf (or typeof in C#) but they perform two completely different things.

In VB there are also two kind of GetType() calls, the object.GetType() method which is part of the .Net framework and the VB language specific GetType() operator.

Are you confused yet? Don’t worry, in this article I will try to explain the difference between these operators and the object.GetType() method.

The TypeOf, typeof, GetType operators

The VB TypeOf operator is used together with the Is keyword and is used for checking if an object is of a particular type.

result = TypeOf x Is String

If “x” above is a string then “result” would be True otherwise it is set to False. This operator have existed in VB since long before .Net was born. The equivalent for the TypeOf operator in C# is simply called the is operator.

result = x is string;

The typeof operator in C# on the other hand returns an instance of the System.Type class containing type declarations of the type you pass to it.

Type t = typeof(string);

The VB equivalent of the C# typeof operator is the GetType operator.

Dim t As Type = GetType(String)

The reason for the different names are simply because of the fact that TypeOf was already a reserved keyword in VB and I guess the C# team didn’t have VB in their mind when they designed C#. 🙂

The difference between Object.GetType and the GetType operator

On a trivial level, the Object.GetType() method operates on an object instance while the GetType (and C#’s typeof) operator operates on a type.

Dim s As String
Dim t As Type = s.GetType()
Dim t2 As Type = GetType(String)

There are no differences between “t” and “t2” in the above code. So why do we have to have both of them? Well, you might not know what type a certain reference is made of in which case you obviously can’t use the GetType operator since that requires that you pass the type. Have a look at the following example:

Public Class MyBaseClass
End Class

Public Class MyDerivedClass
  Inherits MyBaseClass
End Class

Module Test
  Public Sub ShowType(ByVal obj As MyBaseClass)
    Dim t As Type = obj.GetType()
    Dim t2 As Type = GetType(MyBaseClass)
  End Sub

  Public Sub Main()
    Dim myObject As New MyDerivedClass
  End Sub
End Module

In this example the ShowType method takes a MyBaseClass parameter and t2 uses the GetType operator to get the System.Type representation of MyBaseClass. So when you write out t2 to the console it will write MyBaseClass. However when you use the GetType method on the obj parameter it will write MyDerivedClass to the console. It is legal to pass a reference to MyDerivedClass to the ShowType method since it inherits from MyBaseClass so that will be a widening conversion. So inside the ShowType method we will never know if the parameter contains the type we have declared it as or if it contains any subclass of it, to find out we need to use the Object.GetType method.

Another difference between the method and the operator is when you’re dealing with value types.

Dim i As Integer
Dim t As Type = i.GetType()
Dim t2 As Type = GetType(Integer)

In order to call i.GetType() above i must first be converted to an Object which means a boxing conversion has to be done. Using the operator no boxing conversions are made which in this case makes that call faster.

I hope this removes some of the confusion about this subject, if not please feel free to leave a comment.

Have fun!

3 thoughts on “GetType and TypeOf confusion

  1. Not that it has anything to do with this article but that was the most ridiculous statement I’ve heard. Android is an operating system which contains it’s own API which you must use to write an app that runs on Android unless you go for the least most common denominator and write HTML5 apps.

    Saying that there are no such thing as an Android app is like saying that there are no Windows applications or Mac OS X applications.

    Write an app for Android, using Java, and try to run it on an iPhone and see how well that goes, or do the opposite write an app for iPhone using Objective-C and try to run that on an Android device.

Leave a Reply

Your email address will not be published. Required fields are marked *