<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>deepdark.net - James Green's Blog - C# 3.0</title>
    <link>http://deepdark.net/</link>
    <description>.NET, SQL Server and *.*</description>
    <image>
      <url>http://deepdark.net/files/deepdark.jpg</url>
      <title>deepdark.net - James Green's Blog - C# 3.0</title>
      <link>http://deepdark.net/</link>
    </image>
    <language>en-us</language>
    <copyright>James Green</copyright>
    <lastBuildDate>Tue, 01 Apr 2008 11:25:00 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>James.Green@deepdark.net</managingEditor>
    <webMaster>James.Green@deepdark.net</webMaster>
    <item>
      <trackback:ping>http://deepdark.net/Trackback.aspx?guid=5d5addad-0186-4e0d-a8ce-4da5400da852</trackback:ping>
      <pingback:server>http://deepdark.net/pingback.aspx</pingback:server>
      <pingback:target>http://deepdark.net/PermaLink,guid,5d5addad-0186-4e0d-a8ce-4da5400da852.aspx</pingback:target>
      <dc:creator>James Green</dc:creator>
      <wfw:comment>http://deepdark.net/CommentView,guid,5d5addad-0186-4e0d-a8ce-4da5400da852.aspx</wfw:comment>
      <wfw:commentRss>http://deepdark.net/SyndicationService.asmx/GetEntryCommentsRss?guid=5d5addad-0186-4e0d-a8ce-4da5400da852</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">This is the first in what I hope will become <a href="http://deepdark.net/CategoryView,category,C%23%2B3.0.aspx">a
series on the new language features in C# 3.0</a> used in <a href="http://msdn2.microsoft.com/en-us/library/w0x726c2.aspx">.NET
3.5</a> / <a href="http://msdn2.microsoft.com/en-us/vs2008/default.aspx">Visual Studio
2008</a>.<br /><br />
One thing I am not intending to cover is LINQ.  Just because the <a href="http://www.technorati.com/search/linq?authority=a4&amp;language=en">blogosphere
has been buzzing with LINQ</a> articles since the early days of <i>"Orcas"</i>. 
And with good reason I hasten to add!<br /><br /><b>Where I am starting is with the <font color="#000080" face="Courier New">var </font>keyword.</b><br /><br />
VB6 veterans will remember the <font color="#000080" face="Courier New">Variant </font>type. 
A <font color="#000080" face="Courier New">Variant </font>could contain anything,
even <font color="#000080" face="Courier New">Object</font>.  While this was <i>sort
of</i> useful, my memory of it is as a synonym for:  <i>I can't be bothered,
lets just stick it in a Variant and deal with it later</i>.<br /><br />
There was also a performance impact of using the special Variant type, they were large
in memory and have an overhead of extra runtime checking that added up; like when
assigned inside a loop for example.  They were also a special case in their un-assigned
form, taking on the value <font color="#000080" face="Courier New">Empty </font>(test
with <font color="#000080" face="Courier New">IsEmpty()</font>) vs <font color="#000080" face="Courier New">Nothing </font>(test
with <font color="#000080" face="Courier New">Is Nothing</font>).  
<br /><br />
So when I saw <font color="#000080" face="Courier New">var </font>added to C# I raised
my eyebrows in the way a Fed might, when the beagle sits quietly next to your suitcase
at the airport.<br /><br />
Most of the time you see it in the samples, it is used when returning an Anonymous
Type from a LINQ query.  And this is the clue!  <font color="#000080" face="Courier New">var </font>is
not itself a type, but instead <b>it is a signal to the compiler to infer the type
of an operation, and substitute in the required type</b>.  It does not even have
to be an Anonymous Type.  Consider the following simple example:<br /><br /><font face="Courier New"><font color="#000080">var </font>result = 10 / 2.0;<br /><font color="#0000ff"><font color="#000080">Console</font>.</font>WriteLine(result.ToString());<br /></font><br />
By the time this code is compiled, <font color="#000080" face="Courier New">var </font>is
replaced with <font color="#000080" face="Courier New">double</font>.  In fact,
the Intellisense on result will be correct for it being a <font color="#000080" face="Courier New">double</font>.<br /><br />
To confirm this, looking at those lines of the assembly in <a href="http://www.aisto.com/roeder/dotnet/">Lutz
Roder's Reflector</a> show the following after disassembly:<br /><br /><font face="Courier New"><font color="#000080">double </font>result = 5.0;<br /><font color="#000080">Console</font>.WriteLine(result.ToString());</font><br /><br />
OK, so var can be used independant of Anonymous Types, <b>but why would you want to
be <i>less </i>explicit in typing your variables?</b>  Consider the following
fictitious example:<br /><br /><font color="#000080" face="Courier New">DatabaseRequestService </font><font face="Courier New">req
= </font><font color="#000080" face="Courier New">DatabaseRequestService</font><font face="Courier New">.CreateFrom(value);</font><br /><br />
And compare it with the equivalent line using <font color="#000080" face="Courier New"><font color="#0000ff">var</font></font>: 
<br /><br /><font color="#000080" face="Courier New"><font color="#0000ff">var<font color="#000000"></font></font><font color="#000000">req
= </font>DatabaseRequestService</font><font face="Courier New">.CreateFrom(value);</font><br /><br />
Here, <b><font color="#000080" face="Courier New"><font color="#0000ff">var<font color="#000000"></font></font></font>leads
itself to much more readable syntax</b> with the same typing, Intellisense, and everything
else!<br /><br /><font color="#808080" size="1"><b>Listening To:  Róisín Murphy</b></font><br /><p></p></body>
      <title>The var keyword (C# 3.0) - Nothing at all like VB6 Variant - It's not even a Type!</title>
      <guid isPermaLink="false">http://deepdark.net/PermaLink,guid,5d5addad-0186-4e0d-a8ce-4da5400da852.aspx</guid>
      <link>http://deepdark.net/PermaLink,guid,5d5addad-0186-4e0d-a8ce-4da5400da852.aspx</link>
      <pubDate>Tue, 01 Apr 2008 11:25:00 GMT</pubDate>
      <description>This is the first in what I hope will become &lt;a href="http://deepdark.net/CategoryView,category,C%23%2B3.0.aspx"&gt;a
series on the new language features in C# 3.0&lt;/a&gt; used in &lt;a href="http://msdn2.microsoft.com/en-us/library/w0x726c2.aspx"&gt;.NET
3.5&lt;/a&gt; / &lt;a href="http://msdn2.microsoft.com/en-us/vs2008/default.aspx"&gt;Visual Studio
2008&lt;/a&gt;.&lt;br&gt;
&lt;br&gt;
One thing I am not intending to cover is LINQ.&amp;nbsp; Just because the &lt;a href="http://www.technorati.com/search/linq?authority=a4&amp;amp;language=en"&gt;blogosphere
has been buzzing with LINQ&lt;/a&gt; articles since the early days of &lt;i&gt;"Orcas"&lt;/i&gt;.&amp;nbsp;
And with good reason I hasten to add!&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Where I am starting is with the &lt;font color="#000080" face="Courier New"&gt;var &lt;/font&gt;keyword.&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
VB6 veterans will remember the &lt;font color="#000080" face="Courier New"&gt;Variant &lt;/font&gt;type.&amp;nbsp;
A &lt;font color="#000080" face="Courier New"&gt;Variant &lt;/font&gt;could contain anything,
even &lt;font color="#000080" face="Courier New"&gt;Object&lt;/font&gt;.&amp;nbsp; While this was &lt;i&gt;sort
of&lt;/i&gt; useful, my memory of it is as a synonym for:&amp;nbsp; &lt;i&gt;I can't be bothered,
lets just stick it in a Variant and deal with it later&lt;/i&gt;.&lt;br&gt;
&lt;br&gt;
There was also a performance impact of using the special Variant type, they were large
in memory and have an overhead of extra runtime checking that added up; like when
assigned inside a loop for example.&amp;nbsp; They were also a special case in their un-assigned
form, taking on the value &lt;font color="#000080" face="Courier New"&gt;Empty &lt;/font&gt;(test
with &lt;font color="#000080" face="Courier New"&gt;IsEmpty()&lt;/font&gt;) vs &lt;font color="#000080" face="Courier New"&gt;Nothing &lt;/font&gt;(test
with &lt;font color="#000080" face="Courier New"&gt;Is Nothing&lt;/font&gt;).&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
So when I saw &lt;font color="#000080" face="Courier New"&gt;var &lt;/font&gt;added to C# I raised
my eyebrows in the way a Fed might, when the beagle sits quietly next to your suitcase
at the airport.&lt;br&gt;
&lt;br&gt;
Most of the time you see it in the samples, it is used when returning an Anonymous
Type from a LINQ query.&amp;nbsp; And this is the clue!&amp;nbsp; &lt;font color="#000080" face="Courier New"&gt;var &lt;/font&gt;is
not itself a type, but instead &lt;b&gt;it is a signal to the compiler to infer the type
of an operation, and substitute in the required type&lt;/b&gt;.&amp;nbsp; It does not even have
to be an Anonymous Type.&amp;nbsp; Consider the following simple example:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;&lt;font color="#000080"&gt;var &lt;/font&gt;result = 10 / 2.0;&lt;br&gt;
&lt;font color="#0000ff"&gt;&lt;font color="#000080"&gt;Console&lt;/font&gt;.&lt;/font&gt;WriteLine(result.ToString());&lt;br&gt;
&lt;/font&gt;
&lt;br&gt;
By the time this code is compiled, &lt;font color="#000080" face="Courier New"&gt;var &lt;/font&gt;is
replaced with &lt;font color="#000080" face="Courier New"&gt;double&lt;/font&gt;.&amp;nbsp; In fact,
the Intellisense on result will be correct for it being a &lt;font color="#000080" face="Courier New"&gt;double&lt;/font&gt;.&lt;br&gt;
&lt;br&gt;
To confirm this, looking at those lines of the assembly in &lt;a href="http://www.aisto.com/roeder/dotnet/"&gt;Lutz
Roder's Reflector&lt;/a&gt; show the following after disassembly:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;&lt;font color="#000080"&gt;double &lt;/font&gt;result = 5.0;&lt;br&gt;
&lt;font color="#000080"&gt;Console&lt;/font&gt;.WriteLine(result.ToString());&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
OK, so var can be used independant of Anonymous Types, &lt;b&gt;but why would you want to
be &lt;i&gt;less &lt;/i&gt;explicit in typing your variables?&lt;/b&gt;&amp;nbsp; Consider the following
fictitious example:&lt;br&gt;
&lt;br&gt;
&lt;font color="#000080" face="Courier New"&gt;DatabaseRequestService &lt;/font&gt;&lt;font face="Courier New"&gt;req
= &lt;/font&gt;&lt;font color="#000080" face="Courier New"&gt;DatabaseRequestService&lt;/font&gt;&lt;font face="Courier New"&gt;.CreateFrom(value);&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
And compare it with the equivalent line using &lt;font color="#000080" face="Courier New"&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/font&gt;: 
&lt;br&gt;
&lt;br&gt;
&lt;font color="#000080" face="Courier New"&gt;&lt;font color="#0000ff"&gt;var&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#000000"&gt;req
= &lt;/font&gt;DatabaseRequestService&lt;/font&gt;&lt;font face="Courier New"&gt;.CreateFrom(value);&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
Here, &lt;b&gt;&lt;font color="#000080" face="Courier New"&gt;&lt;font color="#0000ff"&gt;var&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;leads
itself to much more readable syntax&lt;/b&gt; with the same typing, Intellisense, and everything
else!&lt;br&gt;
&lt;br&gt;
&lt;font color="#808080" size="1"&gt;&lt;b&gt;Listening To:&amp;nbsp; Róisín Murphy&lt;/b&gt;&lt;/font&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;</description>
      <comments>http://deepdark.net/CommentView,guid,5d5addad-0186-4e0d-a8ce-4da5400da852.aspx</comments>
      <category>C#</category>
      <category>C# 3.0</category>
      <category>Geeking Out!</category>
      <category>Microsoft.NET</category>
    </item>
  </channel>
</rss>