<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Joacim's view on stuff : GetType</title><link>http://msmvps.com/blogs/joacim/archive/tags/GetType/default.aspx</link><description>Tags: GetType</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>GetType and TypeOf confusion</title><link>http://msmvps.com/blogs/joacim/archive/2009/08/31/gettype-and-typeof-confusion.aspx</link><pubDate>Mon, 31 Aug 2009 05:18:46 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1719641</guid><dc:creator>Joacim Andersson</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/joacim/rsscomments.aspx?PostID=1719641</wfw:commentRss><comments>http://msmvps.com/blogs/joacim/archive/2009/08/31/gettype-and-typeof-confusion.aspx#comments</comments><description>&lt;p&gt;Both VB and C# have an operator called &lt;em&gt;&lt;font color="#0000ff"&gt;TypeOf&lt;/font&gt;&lt;/em&gt; (or &lt;em&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/em&gt; in C#) but they perform two completely different things.&lt;/p&gt;  &lt;p&gt;In VB there are also two kind of &lt;em&gt;GetType&lt;/em&gt;() calls, the &lt;em&gt;object&lt;/em&gt;.GetType() method which is part of the .Net framework and the VB language specific &lt;font color="#0000ff"&gt;GetType&lt;/font&gt;() &lt;em&gt;operator&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;Are you confused yet? Don’t worry, in this article I will try to explain the difference between these operators and the &lt;em&gt;object&lt;/em&gt;.GetType() method.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The TypeOf, typeof, GetType operators&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The VB &lt;font color="#0000ff"&gt;TypeOf&lt;/font&gt; operator is used together with the &lt;font color="#0000ff"&gt;Is&lt;/font&gt; keyword and is used for checking if an object is of a particular type.&lt;/p&gt;  &lt;pre class="code"&gt;result = &lt;span style="color:blue;"&gt;TypeOf &lt;/span&gt;x &lt;span style="color:blue;"&gt;Is String&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;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 &lt;font color="#0000ff"&gt;TypeOf &lt;/font&gt;operator in C# is simply called the &lt;font color="#0000ff"&gt;is&lt;/font&gt; operator.&lt;/p&gt;

&lt;pre class="code"&gt;result = x &lt;span style="color:blue;"&gt;is string&lt;/span&gt;;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The &lt;font color="#0000ff"&gt;typeof&lt;/font&gt; 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.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:#2b91af;"&gt;Type &lt;/span&gt;t = &lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:blue;"&gt;string&lt;/span&gt;);&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The VB equivalent of the C# typeof operator is the GetType operator.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;t &lt;span style="color:blue;"&gt;As &lt;/span&gt;Type = &lt;span style="color:blue;"&gt;GetType&lt;/span&gt;(&lt;span style="color:blue;"&gt;String&lt;/span&gt;)&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;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#. :)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The difference between Object.GetType and the GetType operator&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;s &lt;span style="color:blue;"&gt;As String
Dim &lt;/span&gt;t &lt;span style="color:blue;"&gt;As &lt;/span&gt;Type = s.GetType()
&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;t2 &lt;span style="color:blue;"&gt;As &lt;/span&gt;Type = &lt;span style="color:blue;"&gt;GetType&lt;/span&gt;(&lt;span style="color:blue;"&gt;String&lt;/span&gt;)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Public Class &lt;/span&gt;MyBaseClass
&lt;span style="color:blue;"&gt;End Class

Public Class &lt;/span&gt;MyDerivedClass
  &lt;span style="color:blue;"&gt;Inherits &lt;/span&gt;MyBaseClass
&lt;span style="color:blue;"&gt;End Class

Module &lt;/span&gt;Test
  &lt;span style="color:blue;"&gt;Public Sub &lt;/span&gt;ShowType(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;obj &lt;span style="color:blue;"&gt;As &lt;/span&gt;MyBaseClass)
    &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;t &lt;span style="color:blue;"&gt;As &lt;/span&gt;Type = obj.GetType()
    &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;t2 &lt;span style="color:blue;"&gt;As &lt;/span&gt;Type = &lt;span style="color:blue;"&gt;GetType&lt;/span&gt;(MyBaseClass)
    Console.WriteLine(t)
    Console.WriteLine(t2)
  &lt;span style="color:blue;"&gt;End Sub

  Public Sub &lt;/span&gt;Main()
    &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;myObject &lt;span style="color:blue;"&gt;As New &lt;/span&gt;MyDerivedClass
    ShowType(myObject)
    Console.ReadLine()
  &lt;span style="color:blue;"&gt;End Sub
End Module&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;In this example the &lt;em&gt;ShowType&lt;/em&gt; method takes a &lt;em&gt;MyBaseClass&lt;/em&gt; parameter and &lt;em&gt;t2&lt;/em&gt; uses the GetType operator to get the System.Type representation of &lt;em&gt;MyBaseClass.&lt;/em&gt; So when you write out &lt;em&gt;t2&lt;/em&gt; to the console it will write &lt;strong&gt;MyBaseClass&lt;/strong&gt;. However when you use the GetType method on the &lt;em&gt;obj&lt;/em&gt; parameter it will write &lt;strong&gt;MyDerivedClass&lt;/strong&gt; to the console. It is legal to pass a reference to &lt;em&gt;MyDerivedClass&lt;/em&gt; to the &lt;em&gt;ShowType&lt;/em&gt; method since it inherits from &lt;em&gt;MyBaseClass&lt;/em&gt; so that will be a &lt;a href="http://msmvps.com/blogs/joacim/archive/2009/08/31/boxing-and-unboxing-in-net.aspx"&gt;widening conversion&lt;/a&gt;. So inside the &lt;em&gt;ShowType &lt;/em&gt;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 &lt;em&gt;Object.&lt;/em&gt;GetType method.&lt;/p&gt;

&lt;p&gt;Another difference between the method and the operator is when you’re dealing with value types.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;i &lt;span style="color:blue;"&gt;As Integer
Dim &lt;/span&gt;t &lt;span style="color:blue;"&gt;As &lt;/span&gt;Type = i.GetType()
&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;t2 &lt;span style="color:blue;"&gt;As &lt;/span&gt;Type = &lt;span style="color:blue;"&gt;GetType&lt;/span&gt;(&lt;span style="color:blue;"&gt;Integer&lt;/span&gt;)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;In order to call &lt;em&gt;i.GetType()&lt;/em&gt; above &lt;em&gt;i&lt;/em&gt; must first be converted to an Object which means a &lt;a href="http://msmvps.com/blogs/joacim/archive/2009/08/31/boxing-and-unboxing-in-net.aspx"&gt;boxing conversion&lt;/a&gt; has to be done. Using the operator no boxing conversions are made which in this case makes that call faster.&lt;/p&gt;

&lt;p&gt;I hope this removes some of the confusion about this subject, if not please feel free to leave a comment.&lt;/p&gt;

&lt;p&gt;Have fun!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1719641" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/joacim/archive/tags/vb/default.aspx">vb</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/visual+basic/default.aspx">visual basic</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/.net/default.aspx">.net</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/c_2300_/default.aspx">c#</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/GetType/default.aspx">GetType</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/TypeOf/default.aspx">TypeOf</category></item></channel></rss>