{"id":303,"date":"2010-09-24T07:22:34","date_gmt":"2010-09-24T12:22:34","guid":{"rendered":"http:\/\/hoolihan.net\/blog-tim\/?p=303"},"modified":"2010-09-24T07:29:43","modified_gmt":"2010-09-24T12:29:43","slug":"understanding-types-in-net-part-1-var-keyword","status":"publish","type":"post","link":"http:\/\/hoolihan.net\/blog-tim\/2010\/09\/24\/understanding-types-in-net-part-1-var-keyword\/","title":{"rendered":"Understanding Types in .Net Part 1: var keyword"},"content":{"rendered":"<p>It&#8217;s surprising to me how many developers don&#8217;t understand the .Net type system.  So I decided to write a series of posts on of some of the misconceptions.<\/p>\n<p>The &#8220;var&#8221; keyword, introduced in C# 3 is implicit typing.  For all intents and purposes, this is the same as strong typing.  <strong>It is strongly typed at runtime.<\/strong>  Implicit typing means that at compile time, the type of the variable is determined based on what is originally assigned to it.  You can sort of think of it like a text macro in C.  In that metaphor, the token var is replaced with the appropriate type.  If the compiler determines that variable is a string, then you can not later assign an int to variable, the same as if you had declared it a string in the first place.<\/p>\n<p>The intent was to make typing easier with some of the linq types and generics.  See the following lines of code that do the same thing:<\/p>\n<pre>\r\n<code>List&lt;ICustomer&lt;Retail&gt;&gt; Customers = new List&lt;Customer&lt;Retail&gt;&gt;();\r\n\r\n\/\/ add customer here\r\n\r\n\/\/ old way\r\nIQueryable&lt;ICustomer&lt;Retail&gt;&gt; lateCustomers = \r\n                              Customers.Where(c =&gt; c.Bills.UpToDate == true);\r\n\r\n\/\/ new way using var, \r\n\/\/   c is still typed as IQueryable&lt;ICustomer&lt;Retail&gt;&gt;\r\nvar c = Customers.Where(c =&gt; c.Bills.UpToDate == true);<\/code>\r\n<\/pre>\n<p>Here are some unit tests demonstrating that var is still strongly typed.<\/p>\n<pre>\r\n<code>using System;\r\nusing System.Collections.Generic;\r\nusing NUnit.Framework;\r\n\r\nnamespace Tests\r\n{\r\n    [TestFixture]\r\n    class TempTest\r\n    {\r\n        [Test]\r\n        public void VarTypeIsStatic()\r\n        {\r\n            var v = \"foo\";\r\n            string s = v;\r\n            \/\/ v = 3; \/\/This would be illegal\r\n            Assert.AreSame(v.GetType(), s.GetType());\r\n            Assert.AreSame(v,s);\r\n        }\r\n    }\r\n}<\/code>\r\n<\/pre>\n<p>Hopefully, this becomes more clear to developers in .Net 4.  After all, why would the dynamic keyword be added if var was weakly typed?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s surprising to me how many developers don&#8217;t understand the .Net type system. So I decided to write a series of posts on of some of the misconceptions. The &#8220;var&#8221; keyword, introduced in C# 3 is implicit typing. For all intents and purposes, this is the same as strong typing. It is strongly typed at [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[41,111,116],"class_list":["post-303","post","type-post","status-publish","format-standard","hentry","category-programming","tag-net","tag-li","tag-types"],"_links":{"self":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/comments?post=303"}],"version-history":[{"count":0,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/303\/revisions"}],"wp:attachment":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/media?parent=303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/categories?post=303"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/tags?post=303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}