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);
Dim t As Type = GetType(String)
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) Console.WriteLine(t) Console.WriteLine(t2) End Sub Public Sub Main() Dim myObject As New MyDerivedClass ShowType(myObject) Console.ReadLine() 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.