{"id":66,"date":"2009-10-08T20:30:59","date_gmt":"2009-10-09T03:30:59","guid":{"rendered":"http:\/\/www.nerdhive.org\/glob\/?p=66"},"modified":"2009-10-08T20:30:59","modified_gmt":"2009-10-09T03:30:59","slug":"syntactic-sugar-c-var-keyword","status":"publish","type":"post","link":"http:\/\/www.soleillapierre.ca\/blog\/?p=66","title":{"rendered":"Syntactic sugar: C# &#8216;var&#8217; keyword"},"content":{"rendered":"<p>Learned this recently. In .NET 3.5, you can substitute &#8216;var&#8217; for any type name that can be determined at compile time. So instead of typing (for example):<\/p>\n<div class=\"codecolorer-container cpp vibrant\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"cpp codecolorer\">&nbsp; &nbsp; Dictionary<span class=\"sy1\">&lt;<\/span>MyKeyType, MyValueType<span class=\"sy1\">&gt;<\/span> foo <span class=\"sy1\">=<\/span> <span class=\"kw3\">new<\/span> Dictionary<span class=\"sy1\">&lt;<\/span>MyKeyType, MyValueType<span class=\"sy1\">&gt;<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>I can instead just type:<\/p>\n<div class=\"codecolorer-container cpp vibrant\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"cpp codecolorer\">&nbsp; &nbsp; var foo <span class=\"sy1\">=<\/span> <span class=\"kw3\">new<\/span> Dictionary<span class=\"sy1\">&lt;<\/span>MyKeyType, MyValueType<span class=\"sy1\">&gt;<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>This isn&#8217;t the same thing as making &#8216;foo&#8217; be of type object; foo is still strongly typed. You just don&#8217;t have to enter the type twice.<\/p>\n<p>It&#8217;s a minor thing, but type names can get long in C#, especially if you use generics, and I like anything that reduces redundant code entry.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learned this recently. In .NET 3.5, you can substitute &#8216;var&#8217; for any type name that can be determined at compile time. So instead of typing (for example): 1&nbsp; &nbsp; Dictionary&lt;MyKeyType, MyValueType&gt; foo = new Dictionary&lt;MyKeyType, MyValueType&gt;&#40;&#41;; I can instead just type: 1&nbsp; &nbsp; var foo = new Dictionary&lt;MyKeyType, MyValueType&gt;&#40;&#41;; This isn&#8217;t the same thing as [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-66","post","type-post","status-publish","format-standard","hentry","category-softwarehacking"],"_links":{"self":[{"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=\/wp\/v2\/posts\/66","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=66"}],"version-history":[{"count":0,"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=\/wp\/v2\/posts\/66\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=66"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=66"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.soleillapierre.ca\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}