<?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:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>MICHEL'S WEBLOG - .NET 3.5</title>
    <link>http://www.michelvandervlugt.net/</link>
    <description>MICHELVANDERVLUGT.NET</description>
    <language>en-us</language>
    <copyright>Michel van der Vlugt</copyright>
    <lastBuildDate>Wed, 21 May 2008 19:06:22 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 1.9.6264.0</generator>
    <managingEditor>blog@michelvandervlugt.net</managingEditor>
    <webMaster>blog@michelvandervlugt.net</webMaster>
    <item>
      <trackback:ping>http://www.michelvandervlugt.net/Trackback.aspx?guid=15329cb0-5a2b-4c4a-b536-6d36db6cf8db</trackback:ping>
      <pingback:server>http://www.michelvandervlugt.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.michelvandervlugt.net/PermaLink,guid,15329cb0-5a2b-4c4a-b536-6d36db6cf8db.aspx</pingback:target>
      <dc:creator>Michel van der Vlugt</dc:creator>
      <wfw:comment>http://www.michelvandervlugt.net/CommentView,guid,15329cb0-5a2b-4c4a-b536-6d36db6cf8db.aspx</wfw:comment>
      <wfw:commentRss>http://www.michelvandervlugt.net/SyndicationService.asmx/GetEntryCommentsRss?guid=15329cb0-5a2b-4c4a-b536-6d36db6cf8db</wfw:commentRss>
      <slash:comments>8</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This week I ran into a quite unexpected exception that was thrown when I tried to
remove an object from my LINQ ToSQL DataContext:
</p>
        <blockquote>
          <p>
System.InvalidOperationException: An attempt was made to remove a relationship between
a Master and a Detail. However, one of the relationship's foreign keys (Detail.MasterId)
cannot be set to null..
</p>
        </blockquote>
        <p>
The example in which this occurred isn't very exotic, just a Master table and a Detail
table and a relationship with a foreign key MasterId in the Detail table (which doesn't
allow null's). So it should be perfectly valid to remove a row from the Detail table
and submit the changes. 
</p>
        <p>
  
</p>
        <p>
Unfortunately there is a problem with this in LINQ To SQL, which is best explained
by <a href="http://blogs.msdn.com/bethmassi/archive/2007/10/02/linq-to-sql-and-one-to-many-relationships.aspx" target="_blank">this</a> post
of Beth Massi. She states that to work around this problem you have to manually alter
the dbml using an XML editor and add <font face="Courier New">DeleteOnNull="true"</font> to
the association. And yes, this solves the problem.  
</p>
        <p>
  
</p>
        <p>
Beth discovered this problem in VS2008 Beta 2 and it still exists in the final release.
Let's hope the problem is solved with the upcoming SP1...
</p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=15329cb0-5a2b-4c4a-b536-6d36db6cf8db" />
      </body>
      <title>LINQ To SQL: Exception when removing child object from Master-Detail relationship</title>
      <guid isPermaLink="false">http://www.michelvandervlugt.net/PermaLink,guid,15329cb0-5a2b-4c4a-b536-6d36db6cf8db.aspx</guid>
      <link>http://www.michelvandervlugt.net/PermaLink,guid,15329cb0-5a2b-4c4a-b536-6d36db6cf8db.aspx</link>
      <pubDate>Wed, 21 May 2008 19:06:22 GMT</pubDate>
      <description>&lt;p&gt;
This week I ran into a quite unexpected exception that was thrown when I tried to
remove an object from my LINQ ToSQL DataContext:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
System.InvalidOperationException: An attempt was made to remove a relationship between
a Master and a Detail. However, one of the relationship's foreign keys (Detail.MasterId)
cannot be set to null..
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The example in which this occurred isn't very exotic, just a Master table and a Detail
table and a relationship with a foreign key MasterId in the Detail table (which doesn't
allow null's). So it should be perfectly valid to remove a row from the Detail table
and submit the changes. 
&lt;p&gt;
&amp;nbsp; 
&lt;p&gt;
Unfortunately there is a problem with this in LINQ To SQL, which is best explained
by &lt;a href="http://blogs.msdn.com/bethmassi/archive/2007/10/02/linq-to-sql-and-one-to-many-relationships.aspx" target="_blank"&gt;this&lt;/a&gt; post
of Beth Massi. She states that to work around this problem you have to manually alter
the dbml using an XML editor and add &lt;font face="Courier New"&gt;DeleteOnNull="true"&lt;/font&gt; to
the association. And yes, this solves the problem.&amp;nbsp; 
&lt;p&gt;
&amp;nbsp; 
&lt;p&gt;
Beth discovered this problem in VS2008 Beta 2 and it still exists in the final release.
Let's hope the problem is solved with the upcoming SP1...
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=15329cb0-5a2b-4c4a-b536-6d36db6cf8db" /&gt;</description>
      <comments>http://www.michelvandervlugt.net/CommentView,guid,15329cb0-5a2b-4c4a-b536-6d36db6cf8db.aspx</comments>
      <category>.NET 3.5</category>
    </item>
    <item>
      <trackback:ping>http://www.michelvandervlugt.net/Trackback.aspx?guid=881d538d-c9c3-49c2-8f7a-2633f0d48839</trackback:ping>
      <pingback:server>http://www.michelvandervlugt.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.michelvandervlugt.net/PermaLink,guid,881d538d-c9c3-49c2-8f7a-2633f0d48839.aspx</pingback:target>
      <dc:creator>Michel van der Vlugt</dc:creator>
      <wfw:comment>http://www.michelvandervlugt.net/CommentView,guid,881d538d-c9c3-49c2-8f7a-2633f0d48839.aspx</wfw:comment>
      <wfw:commentRss>http://www.michelvandervlugt.net/SyndicationService.asmx/GetEntryCommentsRss?guid=881d538d-c9c3-49c2-8f7a-2633f0d48839</wfw:commentRss>
      <slash:comments>11</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <style type="text/css">
    .csharpcode, .csharpcode pre
    {
        font-size: small;
        color: black;
        font-family: consolas, "Courier New" , courier, monospace;
        background-color: #ffffff; /*white-space: pre;*/
    }
    .csharpcode pre
    {
        margin: 0em;
    }
    .csharpcode .rem
    {
        color: #008000;
    }
    .csharpcode .kwrd
    {
        color: #0000ff;
    }
    .csharpcode .str
    {
        color: #006080;
    }
    .csharpcode .op
    {
        color: #0000c0;
    }
    .csharpcode .preproc
    {
        color: #cc6633;
    }
    .csharpcode .asp
    {
        background-color: #ffff00;
    }
    .csharpcode .html
    {
        color: #800000;
    }
    .csharpcode .attr
    {
        color: #ff0000;
    }
    .csharpcode .alt
    {
        background-color: #f4f4f4;
        width: 100%;
        margin: 0em;
    }
    .csharpcode .lnum
    {
        color: #606060;
    }
</style>
        <p>
LINQ uses a Deferred Execution model which means that nothing really happens until
the results of the query are accessed, e.g. in a for(each)-loop. One of the advantages
of this model is that you can compose complex queries in multiple steps to make them
more readable. So from a execution point of view I expected that it should not matter
whether you create the query in one complex statement, or in multiple smaller statements.
But unfortunately that's not always the case...
</p>
        <p>
To see the difference I've created an XML document and two simple queries that create
Album objects when the where-clause matches. 
</p>
        <p>
The 'complex' query:
</p>
        <p>
        </p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>var query1 = <span class="kwrd">from</span> a <span class="kwrd">in</span> albums.Descendants(<span class="str">"Album"</span>)</pre>
          <pre>
            <span class="lnum"> 2: </span>
            <span class="kwrd">where</span> a.Element(<span class="str">"Artist"</span>).Value
== <span class="str">"Radiohead"</span></pre>
          <pre>
            <span class="lnum"> 3: </span>
            <span class="kwrd">select</span>
            <span class="kwrd">new</span> Album</pre>
          <pre>
            <span class="lnum"> 4: </span> {</pre>
          <pre>
            <span class="lnum"> 5: </span> Artist = a.Element(<span class="str">"Artist"</span>).Value,</pre>
          <pre>
            <span class="lnum"> 6: </span> Title = a.Element(<span class="str">"Title"</span>).Value</pre>
          <pre>
            <span class="lnum"> 7: </span> };</pre>
        </div>
        <p>
        </p>
        <p>
The decomposed, more readable query:
</p>
        <p>
        </p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>var query2 = <span class="kwrd">from</span> a <span class="kwrd">in</span> albums.Descendants(<span class="str">"Album"</span>)</pre>
          <pre>
            <span class="lnum"> 2: </span>
            <span class="kwrd">select</span>
            <span class="kwrd">new</span> Album</pre>
          <pre>
            <span class="lnum"> 3: </span> {</pre>
          <pre>
            <span class="lnum"> 4: </span> Artist = a.Element(<span class="str">"Artist"</span>).Value,</pre>
          <pre>
            <span class="lnum"> 5: </span> Title = a.Element(<span class="str">"Title"</span>).Value</pre>
          <pre>
            <span class="lnum"> 6: </span> };</pre>
          <pre>
            <span class="lnum"> 7: </span> </pre>
          <pre>
            <span class="lnum"> 8: </span>query2 = <span class="kwrd">from</span> a <span class="kwrd">in</span> query2</pre>
          <pre>
            <span class="lnum"> 9: </span>
            <span class="kwrd">where</span> a.Artist == <span class="str">"Radiohead"</span></pre>
          <pre>
            <span class="lnum"> 10: </span>
            <span class="kwrd">select</span> a;</pre>
        </div>
        <p>
        </p>
        <p>
The result from both queries is exactly the same, but they execute differently. What
happens in Query2 is that first a list of <u>all</u> Album objects is created and
then the ‘where’ part is evaluated over each object. This is different compared to
Query1 in which an Album object is only created when it matches the ‘where’ part.
So in this example Query1 is more efficient.
</p>
        <p>
Is this what we should expect of deferred execution with LINQ? Yes, at least on implementations
based on IEnumerable&lt;T&gt;. What basically happens is that a chain of methods is
created and executed in the same order as added to the query. This makes Query1: albums.Where(...).Select(...)
and Query2: albums.Select(...).Where(...).
</p>
        <p>
With implementations based on IQueryable&lt;T&gt;, like LINQ To SQL, this is different
in a way that it uses an Expression Tree to analyze/optimize the query. Using the
same two queries with LINQ To SQL both queries execute exactly the same! Only one
SQL statement is send to database and they both include the where-clause, which make
them equally efficient.
</p>
        <p>
In this example it isn't that much a problem but when more data is involved this is
definitely something to be aware of when composing LINQ queries. 
</p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=881d538d-c9c3-49c2-8f7a-2633f0d48839" />
      </body>
      <title>Deferred Execution behavior in LINQ providers</title>
      <guid isPermaLink="false">http://www.michelvandervlugt.net/PermaLink,guid,881d538d-c9c3-49c2-8f7a-2633f0d48839.aspx</guid>
      <link>http://www.michelvandervlugt.net/PermaLink,guid,881d538d-c9c3-49c2-8f7a-2633f0d48839.aspx</link>
      <pubDate>Mon, 07 Apr 2008 17:21:43 GMT</pubDate>
      <description> &lt;style type="text/css"&gt;
    .csharpcode, .csharpcode pre
    {
        font-size: small;
        color: black;
        font-family: consolas, "Courier New" , courier, monospace;
        background-color: #ffffff; /*white-space: pre;*/
    }
    .csharpcode pre
    {
        margin: 0em;
    }
    .csharpcode .rem
    {
        color: #008000;
    }
    .csharpcode .kwrd
    {
        color: #0000ff;
    }
    .csharpcode .str
    {
        color: #006080;
    }
    .csharpcode .op
    {
        color: #0000c0;
    }
    .csharpcode .preproc
    {
        color: #cc6633;
    }
    .csharpcode .asp
    {
        background-color: #ffff00;
    }
    .csharpcode .html
    {
        color: #800000;
    }
    .csharpcode .attr
    {
        color: #ff0000;
    }
    .csharpcode .alt
    {
        background-color: #f4f4f4;
        width: 100%;
        margin: 0em;
    }
    .csharpcode .lnum
    {
        color: #606060;
    }
&lt;/style&gt;
&lt;p&gt;
LINQ uses a Deferred Execution model which means that nothing really happens until
the results of the query are accessed, e.g. in a for(each)-loop. One of the advantages
of this model is that you can compose complex queries in multiple steps to make them
more readable. So from a execution point of view I expected that it should not matter
whether you create the query in one complex statement, or in multiple smaller statements.
But unfortunately that's not always the case...
&lt;/p&gt;
&lt;p&gt;
To see the difference I've created an XML document and two simple queries that create
Album objects when the where-clause matches. 
&lt;/p&gt;
&lt;p&gt;
The 'complex' query:
&lt;/p&gt;
&lt;p&gt;
&lt;div class="csharpcode"&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 1: &lt;/span&gt;var query1 = &lt;span class="kwrd"&gt;from&lt;/span&gt; a &lt;span class="kwrd"&gt;in&lt;/span&gt; albums.Descendants(&lt;span class="str"&gt;"Album"&lt;/span&gt;)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 2: &lt;/span&gt; &lt;span class="kwrd"&gt;where&lt;/span&gt; a.Element(&lt;span class="str"&gt;"Artist"&lt;/span&gt;).Value
== &lt;span class="str"&gt;"Radiohead"&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 3: &lt;/span&gt; &lt;span class="kwrd"&gt;select&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Album&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 4: &lt;/span&gt; {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 5: &lt;/span&gt; Artist = a.Element(&lt;span class="str"&gt;"Artist"&lt;/span&gt;).Value,&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 6: &lt;/span&gt; Title = a.Element(&lt;span class="str"&gt;"Title"&lt;/span&gt;).Value&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 7: &lt;/span&gt; };&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
The decomposed, more readable query:
&lt;/p&gt;
&lt;p&gt;
&lt;div class="csharpcode"&gt;&lt;pre&gt;&lt;span class="lnum"&gt; 1: &lt;/span&gt;var query2 = &lt;span class="kwrd"&gt;from&lt;/span&gt; a &lt;span class="kwrd"&gt;in&lt;/span&gt; albums.Descendants(&lt;span class="str"&gt;"Album"&lt;/span&gt;)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 2: &lt;/span&gt; &lt;span class="kwrd"&gt;select&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Album&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 3: &lt;/span&gt; {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 4: &lt;/span&gt; Artist = a.Element(&lt;span class="str"&gt;"Artist"&lt;/span&gt;).Value,&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 5: &lt;/span&gt; Title = a.Element(&lt;span class="str"&gt;"Title"&lt;/span&gt;).Value&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 6: &lt;/span&gt; };&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 7: &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 8: &lt;/span&gt;query2 = &lt;span class="kwrd"&gt;from&lt;/span&gt; a &lt;span class="kwrd"&gt;in&lt;/span&gt; query2&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 9: &lt;/span&gt; &lt;span class="kwrd"&gt;where&lt;/span&gt; a.Artist == &lt;span class="str"&gt;"Radiohead"&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt; 10: &lt;/span&gt; &lt;span class="kwrd"&gt;select&lt;/span&gt; a;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
The result from both queries is exactly the same, but they execute differently. What
happens in Query2 is that first a list of &lt;u&gt;all&lt;/u&gt; Album objects is created and
then the ‘where’ part is evaluated over each object. This is different compared to
Query1 in which an Album object is only created when it matches the ‘where’ part.
So in this example Query1 is more efficient.
&lt;/p&gt;
&lt;p&gt;
Is this what we should expect of deferred execution with LINQ? Yes, at least on implementations
based on IEnumerable&amp;lt;T&amp;gt;. What basically happens is that a chain of methods is
created and executed in the same order as added to the query. This makes Query1: albums.Where(...).Select(...)
and Query2: albums.Select(...).Where(...).
&lt;/p&gt;
&lt;p&gt;
With implementations based on IQueryable&amp;lt;T&amp;gt;, like LINQ To SQL, this is different
in a way that it uses an Expression Tree to analyze/optimize the query. Using the
same two queries with LINQ To SQL both queries execute exactly the same! Only one
SQL statement is send to database and they both include the where-clause, which make
them equally efficient.
&lt;/p&gt;
&lt;p&gt;
In this example it isn't that much a problem but when more data is involved this is
definitely something to be aware of when composing LINQ queries. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=881d538d-c9c3-49c2-8f7a-2633f0d48839" /&gt;</description>
      <comments>http://www.michelvandervlugt.net/CommentView,guid,881d538d-c9c3-49c2-8f7a-2633f0d48839.aspx</comments>
      <category>.NET 3.5;C#</category>
    </item>
    <item>
      <trackback:ping>http://www.michelvandervlugt.net/Trackback.aspx?guid=85f2509d-684f-430a-a0ab-e8eb1ce132af</trackback:ping>
      <pingback:server>http://www.michelvandervlugt.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.michelvandervlugt.net/PermaLink,guid,85f2509d-684f-430a-a0ab-e8eb1ce132af.aspx</pingback:target>
      <dc:creator>Michel van der Vlugt</dc:creator>
      <wfw:comment>http://www.michelvandervlugt.net/CommentView,guid,85f2509d-684f-430a-a0ab-e8eb1ce132af.aspx</wfw:comment>
      <wfw:commentRss>http://www.michelvandervlugt.net/SyndicationService.asmx/GetEntryCommentsRss?guid=85f2509d-684f-430a-a0ab-e8eb1ce132af</wfw:commentRss>
      <slash:comments>10</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
With the release of Visual Studio 2008 a great new feature will be added: access to
the source code of the .NET Framework libraries! In <a href="http://weblogs.asp.net/scottgu/archive/2007/10/03/releasing-the-source-code-for-the-net-framework-libraries.aspx" target="_blank">this
post</a> of Scott Guthrie he announces that VS2008 will support downloading the source
code from the Internet so you are able to debug the .NET Framework libraries.
</p>
        <p>
This is really a great thing because now we get more insight in how the .NET Framework
is implemented and why some things might not work as expected. Up until now there
is the possibility to use <a href="http://www.aisto.com/roeder/dotnet/" target="_blank">Reflector</a> to
explore the framework libraries, but this will be much easier as it supports
the debug ability...
</p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=85f2509d-684f-430a-a0ab-e8eb1ce132af" />
      </body>
      <title>.NET Framework source code</title>
      <guid isPermaLink="false">http://www.michelvandervlugt.net/PermaLink,guid,85f2509d-684f-430a-a0ab-e8eb1ce132af.aspx</guid>
      <link>http://www.michelvandervlugt.net/PermaLink,guid,85f2509d-684f-430a-a0ab-e8eb1ce132af.aspx</link>
      <pubDate>Sat, 06 Oct 2007 10:16:45 GMT</pubDate>
      <description>&lt;p&gt;
With the release of Visual Studio 2008 a great new feature will be added: access to
the source code of the .NET Framework libraries! In&amp;nbsp;&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/10/03/releasing-the-source-code-for-the-net-framework-libraries.aspx" target="_blank"&gt;this
post&lt;/a&gt; of Scott Guthrie he announces that VS2008 will support downloading the source
code from the Internet so you&amp;nbsp;are able to debug the .NET Framework libraries.
&lt;/p&gt;
&lt;p&gt;
This is really a great thing because now we get more insight in how the .NET Framework
is implemented and why some things might not work as expected. Up until now there
is the possibility to use &lt;a href="http://www.aisto.com/roeder/dotnet/" target="_blank"&gt;Reflector&lt;/a&gt; to
explore&amp;nbsp;the framework libraries, but this will be much easier as it supports
the debug ability...
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=85f2509d-684f-430a-a0ab-e8eb1ce132af" /&gt;</description>
      <comments>http://www.michelvandervlugt.net/CommentView,guid,85f2509d-684f-430a-a0ab-e8eb1ce132af.aspx</comments>
      <category>.NET 3.5</category>
    </item>
    <item>
      <trackback:ping>http://www.michelvandervlugt.net/Trackback.aspx?guid=b631cfe8-178b-40ac-9ce1-4b6777095d37</trackback:ping>
      <pingback:server>http://www.michelvandervlugt.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.michelvandervlugt.net/PermaLink,guid,b631cfe8-178b-40ac-9ce1-4b6777095d37.aspx</pingback:target>
      <dc:creator>Michel van der Vlugt</dc:creator>
      <wfw:comment>http://www.michelvandervlugt.net/CommentView,guid,b631cfe8-178b-40ac-9ce1-4b6777095d37.aspx</wfw:comment>
      <wfw:commentRss>http://www.michelvandervlugt.net/SyndicationService.asmx/GetEntryCommentsRss?guid=b631cfe8-178b-40ac-9ce1-4b6777095d37</wfw:commentRss>
      <slash:comments>9</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In this post I will wrap up some of the smaller but very useful new features of .NET
3.5/C# 3.0. Other features of .NET 3.5 I've already covered are <a href="http://www.michelvandervlugt.net/PermaLink,guid,d0d57645-1310-4c05-9931-8be40e6e0f27.aspx" target="_blank">Extension
Methods</a> and <a href="http://www.michelvandervlugt.net/PermaLink,guid,1090afda-a98a-484b-b963-a40d24eb3ab6.aspx" target="_blank">Lambda
Expressions</a>.
</p>
        <p>
          <em>Object and Collection Initializers</em>
          <br />
This feature makes it easier to create objects and assign values to the properties
of the objects. Until .NET 3.5 you either had to write code that created an object
and initialized each property of the object, or use a constructor that takes a list
of parameters (which had to change of be overloaded when a property was added). The
same is true for creating lists of object, calling the Add method for every new object.
The following example shows that object and collection initializers makes this easier,
without the use of parameter constructors:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e3c78f6c-ff5e-451d-9ebe-0ba282eaf305" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #000000">persons </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> List</span>
              <span style="COLOR: #000000">&lt;</span>
              <span style="COLOR: #000000">Person</span>
              <span style="COLOR: #000000">&gt;</span>
              <span style="COLOR: #000000"> { </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> Person
{ Name </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Person
I</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000"> }, </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> Person
{ Name </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Person
II</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000"> }, </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> Person
{ Name </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Person
III</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000"> }, </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> Person
{ Name </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Person
IV</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000"> } };</span>
            </div>
          </pre>
        </div>
        <p>
In the example above the default constructor (without parameters) of Person will be
used, witch will be executed before the property initialization. In case an alternative
constructor is required, e.g. including a read only Id, the following syntax
can be used:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4e373a4f-9de1-4bc0-8482-52c460a37948" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #000000">Person
person </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> Person(</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">NewGuid</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">)
{ Name </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Person</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000"> };</span>
            </div>
          </pre>
        </div>
        <p>
          <em>Automatic Property get/set</em>
          <br />
The feature of automatic property takes away the burden of explicitly coding
a private field and the get/set of a property, these are generated
automatically by the compiler. The syntax is similar to the way of defining an abstract
property or a property in an interface:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c7fce94c-fe2f-437c-9ca5-4075cb349655" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #0000ff">public</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">class</span>
              <span style="COLOR: #000000"> Person
{ </span>
              <span style="COLOR: #0000ff">public</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">string</span>
              <span style="COLOR: #000000"> Name
{ </span>
              <span style="COLOR: #0000ff">get</span>
              <span style="COLOR: #000000">; </span>
              <span style="COLOR: #0000ff">set</span>
              <span style="COLOR: #000000">;
} }</span>
            </div>
          </pre>
        </div>
        <p>
If necessary the get/set can be changed to limit accessibility, e.g. Name
{ get; private set; }. Of course the object initializers as explained above cannot
be used outside the class anymore.
</p>
        <p>
          <em>Implicitly Typed Local Variables and Arrays</em>
          <br />
With implicitly typed variables the type of the variable is not defined up front but
inferred by the compiler using the initialization of the variable. Basically this
looks the same as defining the variable as an object, but there is a huge difference:
after initialization the type is fixed and cannot be changed, e.g. by assigning a
value of another type.
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:cfb5dfd6-fb2d-4acc-84ad-f58fc23b918b" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #000000">var
a </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">5</span>
              <span style="COLOR: #000000">; </span>
              <span style="COLOR: #008000">//</span>
              <span style="COLOR: #008000"> int</span>
              <span style="COLOR: #008000">
              </span>
              <span style="COLOR: #000000">var
b </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">5.5</span>
              <span style="COLOR: #000000">; </span>
              <span style="COLOR: #008000">//</span>
              <span style="COLOR: #008000"> double</span>
              <span style="COLOR: #008000">
              </span>
              <span style="COLOR: #000000">var
c </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000"> a </span>
              <span style="COLOR: #000000">+</span>
              <span style="COLOR: #000000"> b; </span>
              <span style="COLOR: #008000">//</span>
              <span style="COLOR: #008000"> double</span>
              <span style="COLOR: #008000">
              </span>
              <span style="COLOR: #000000">var
d </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">int</span>
              <span style="COLOR: #000000">[]
{ </span>
              <span style="COLOR: #000000">1</span>
              <span style="COLOR: #000000">, </span>
              <span style="COLOR: #000000">2</span>
              <span style="COLOR: #000000"> }; </span>
              <span style="COLOR: #008000">//</span>
              <span style="COLOR: #008000"> int
array</span>
              <span style="COLOR: #008000">
              </span>
              <span style="COLOR: #000000">var
e </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000">[]
{ </span>
              <span style="COLOR: #000000">1</span>
              <span style="COLOR: #000000">, </span>
              <span style="COLOR: #000000">2</span>
              <span style="COLOR: #000000"> }; </span>
              <span style="COLOR: #008000">//</span>
              <span style="COLOR: #008000"> implicitly
typed int array</span>
              <span style="COLOR: #008000">
              </span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">foreach</span>
              <span style="COLOR: #000000"> (var
f </span>
              <span style="COLOR: #0000ff">in</span>
              <span style="COLOR: #000000"> e) Console.WriteLine(f); </span>
            </div>
          </pre>
        </div>
        <p>
Using the var keyword is only allowed for local variables, they cannot be used as
class variables for example. Also, they have to be initialized when they are
declared.
</p>
        <p>
The usage of implicitly typed variables as aboveis a bit doubtful I think, in fact it
does make the code less readable (although IntelliSense can help you out). In relation
to LINQ and Anonymous Types it does make more sense though, but more on that later.
</p>
        <p>
          <em>Anonymous Types</em>
          <br />
Anonymous Types are objects that are defined and created on the fly
using object initializers. The compiler actually creates properties of the
initializers which can be used later on. The name of the type remains unknown
and cannot be used within the program.
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:75a26faa-89c9-46bd-a1fd-b349b17abca3" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #000000">var
p1 </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> {
Name </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Person
I</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">, City </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">New
York</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000"> }; Console.WriteLine( </span>
              <span style="COLOR: #0000ff">string</span>
              <span style="COLOR: #000000">.Format(</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">{0}
from {1}</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">,
p1.Name, p1.City)); </span>
            </div>
          </pre>
        </div>
        <p>
Like implicitly Typed Variables the usage of Anonymous Types in this context doesn't
seem to be very useful because you really cannot do much with such
an object. With LINQ the usage will be more frequent, e.g. to display a selection
from an executed query:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:2e09d0b5-2afa-452b-af2e-ca8b89c97688" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #000000">var
nyPersons </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000"> from
q </span>
              <span style="COLOR: #0000ff">in</span>
              <span style="COLOR: #000000"> persons </span>
              <span style="COLOR: #0000ff">where</span>
              <span style="COLOR: #000000"> q.Address.City </span>
              <span style="COLOR: #000000">==</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">New
York</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000"> select </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> {
Name </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000"> q.Name,
City </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000"> q.Address.City
}; </span>
              <span style="COLOR: #0000ff">foreach</span>
              <span style="COLOR: #000000"> (var
nyp </span>
              <span style="COLOR: #0000ff">in</span>
              <span style="COLOR: #000000"> nyPersons)
Console.WriteLine(</span>
              <span style="COLOR: #0000ff">string</span>
              <span style="COLOR: #000000">.Format(</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">{0}
from {1}</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">,
nyp.Name, nyp.City)); </span>
            </div>
          </pre>
        </div>
        <p>
The source code of these examples can be downloaded from here: <a href="http://www.michelvandervlugt.net/content/binary/TypesVariablesAndInitDemo.zip">TypesVariablesAndInitDemo.zip
(,95 KB)</a></p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=b631cfe8-178b-40ac-9ce1-4b6777095d37" />
      </body>
      <title>.NET 3.5: Types, Variables and Initializers</title>
      <guid isPermaLink="false">http://www.michelvandervlugt.net/PermaLink,guid,b631cfe8-178b-40ac-9ce1-4b6777095d37.aspx</guid>
      <link>http://www.michelvandervlugt.net/PermaLink,guid,b631cfe8-178b-40ac-9ce1-4b6777095d37.aspx</link>
      <pubDate>Wed, 15 Aug 2007 19:58:16 GMT</pubDate>
      <description>&lt;p&gt;
In this post I will wrap up some of the smaller but very useful new features of .NET
3.5/C# 3.0. Other features of .NET 3.5 I've already covered are &lt;a href="http://www.michelvandervlugt.net/PermaLink,guid,d0d57645-1310-4c05-9931-8be40e6e0f27.aspx" target=_blank&gt;Extension
Methods&lt;/a&gt;&amp;nbsp;and &lt;a href="http://www.michelvandervlugt.net/PermaLink,guid,1090afda-a98a-484b-b963-a40d24eb3ab6.aspx" target=_blank&gt;Lambda
Expressions&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Object and Collection Initializers&lt;/em&gt;
&lt;br&gt;
This feature makes it easier to create objects and assign values to the properties
of the objects. Until .NET 3.5 you either had to write code that created an object
and initialized each property of the object, or use a constructor that takes a list
of parameters (which had to change of be overloaded when a property was added). The
same is true for creating lists of object, calling the Add method for every new object.
The following example shows that object and collection initializers makes this&amp;nbsp;easier,
without the use of parameter constructors:
&lt;/p&gt;
&lt;div class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e3c78f6c-ff5e-451d-9ebe-0ba282eaf305 contenteditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;pre style="BACKGROUND-COLOR: white"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="COLOR: #000000"&gt;persons &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; List&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;Person&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; { &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; Person
{ Name &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;Person
I&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; }, &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; Person
{ Name &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;Person
II&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; }, &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; Person
{ Name &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;Person
III&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; }, &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; Person
{ Name &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;Person
IV&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; } };&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In the example above the default constructor (without parameters) of Person will be
used, witch will be executed before the property initialization. In case an alternative
constructor is required, e.g. including a read only&amp;nbsp;Id, the following syntax
can be used:
&lt;/p&gt;
&lt;div class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4e373a4f-9de1-4bc0-8482-52c460a37948 contenteditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;pre style="BACKGROUND-COLOR: white"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="COLOR: #000000"&gt;Person
person &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; Person(&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;NewGuid&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;)
{ Name &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;Person&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; };&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;em&gt;Automatic Property get/set&lt;/em&gt;
&lt;br&gt;
The feature of automatic property takes away the burden of explicitly&amp;nbsp;coding
a private field and&amp;nbsp;the get/set of a property,&amp;nbsp;these are&amp;nbsp;generated
automatically by the compiler. The syntax is similar to the way of defining an abstract
property or a property in an interface:
&lt;/p&gt;
&lt;div class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c7fce94c-fe2f-437c-9ca5-4075cb349655 contenteditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;pre style="BACKGROUND-COLOR: white"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; Person
{ &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; Name
{ &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;get&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;set&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;;
} }&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
If necessary the&amp;nbsp;get/set&amp;nbsp;can be changed to limit accessibility, e.g. Name
{ get; private set; }. Of course the object initializers as explained above cannot
be used outside the class anymore.
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Implicitly Typed Local Variables and Arrays&lt;/em&gt;
&lt;br&gt;
With implicitly typed variables the type of the variable is not defined up front but
inferred by the compiler using the initialization of the variable. Basically this
looks the same as&amp;nbsp;defining the variable as an object, but there is a huge difference:
after initialization the type is fixed and cannot be changed, e.g. by assigning a
value of another type.
&lt;/p&gt;
&lt;div class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:cfb5dfd6-fb2d-4acc-84ad-f58fc23b918b contenteditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;pre style="BACKGROUND-COLOR: white"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="COLOR: #000000"&gt;var
a &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;5&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;; &lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//&lt;/span&gt;&lt;span style="COLOR: #008000"&gt; int&lt;/span&gt;&lt;span style="COLOR: #008000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;var
b &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;5.5&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;; &lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//&lt;/span&gt;&lt;span style="COLOR: #008000"&gt; double&lt;/span&gt;&lt;span style="COLOR: #008000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;var
c &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; a &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;+&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; b; &lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//&lt;/span&gt;&lt;span style="COLOR: #008000"&gt; double&lt;/span&gt;&lt;span style="COLOR: #008000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;var
d &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;int&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;[]
{ &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;1&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;, &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;2&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; }; &lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//&lt;/span&gt;&lt;span style="COLOR: #008000"&gt; int
array&lt;/span&gt;&lt;span style="COLOR: #008000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;var
e &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;[]
{ &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;1&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;, &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;2&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; }; &lt;/span&gt;&lt;span style="COLOR: #008000"&gt;//&lt;/span&gt;&lt;span style="COLOR: #008000"&gt; implicitly
typed int array&lt;/span&gt;&lt;span style="COLOR: #008000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;foreach&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; (var
f &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;in&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; e) Console.WriteLine(f); &lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Using the var keyword is only allowed for local variables, they cannot be used as
class variables for example. Also, they&amp;nbsp;have to be initialized when they are
declared.
&lt;/p&gt;
&lt;p&gt;
The usage of implicitly typed variables as aboveis a bit doubtful I think, in fact&amp;nbsp;it
does make the code less readable (although IntelliSense can help you out). In relation
to LINQ and Anonymous Types it does make more sense though, but more on that later.
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Anonymous Types&lt;/em&gt;
&lt;br&gt;
Anonymous Types are&amp;nbsp;objects that are&amp;nbsp;defined and&amp;nbsp;created on the fly
using object initializers. The compiler actually&amp;nbsp;creates properties&amp;nbsp;of the
initializers which&amp;nbsp;can be used later on. The name of the type remains unknown
and cannot be used within the program.
&lt;/p&gt;
&lt;div class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:75a26faa-89c9-46bd-a1fd-b349b17abca3 contenteditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;pre style="BACKGROUND-COLOR: white"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="COLOR: #000000"&gt;var
p1 &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; {
Name &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;Person
I&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;, City &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;New
York&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; }; Console.WriteLine( &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;.Format(&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;{0}
from {1}&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;,
p1.Name, p1.City)); &lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Like implicitly Typed Variables the usage of Anonymous Types in this context doesn't
seem to be&amp;nbsp;very&amp;nbsp;useful because you&amp;nbsp;really cannot do much with such
an object. With LINQ the usage will be more frequent, e.g. to display a selection
from an executed query:
&lt;/p&gt;
&lt;div class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:2e09d0b5-2afa-452b-af2e-ca8b89c97688 contenteditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;pre style="BACKGROUND-COLOR: white"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="COLOR: #000000"&gt;var
nyPersons &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; from
q &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;in&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; persons &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;where&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; q.Address.City &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;==&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;New
York&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; select &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; {
Name &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; q.Name,
City &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; q.Address.City
}; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;foreach&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; (var
nyp &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;in&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; nyPersons)
Console.WriteLine(&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;.Format(&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;{0}
from {1}&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;,
nyp.Name, nyp.City)); &lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The source code of these examples can be downloaded from here: &lt;a href="http://www.michelvandervlugt.net/content/binary/TypesVariablesAndInitDemo.zip"&gt;TypesVariablesAndInitDemo.zip
(,95 KB)&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=b631cfe8-178b-40ac-9ce1-4b6777095d37" /&gt;</description>
      <comments>http://www.michelvandervlugt.net/CommentView,guid,b631cfe8-178b-40ac-9ce1-4b6777095d37.aspx</comments>
      <category>.NET 3.5</category>
    </item>
    <item>
      <trackback:ping>http://www.michelvandervlugt.net/Trackback.aspx?guid=1090afda-a98a-484b-b963-a40d24eb3ab6</trackback:ping>
      <pingback:server>http://www.michelvandervlugt.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.michelvandervlugt.net/PermaLink,guid,1090afda-a98a-484b-b963-a40d24eb3ab6.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.michelvandervlugt.net/CommentView,guid,1090afda-a98a-484b-b963-a40d24eb3ab6.aspx</wfw:comment>
      <wfw:commentRss>http://www.michelvandervlugt.net/SyndicationService.asmx/GetEntryCommentsRss?guid=1090afda-a98a-484b-b963-a40d24eb3ab6</wfw:commentRss>
      <slash:comments>11</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In addition to <a href="http://www.michelvandervlugt.net/PermaLink,guid,d0d57645-1310-4c05-9931-8be40e6e0f27.aspx" target="_blank">my
first post</a> about the new .NET 3.5 language features my second post in this
series will be about Lambda Expressions. Lambda Expressions are basically
a follow up to Anonymous Methods (in-line delegates), which were introduced in .NET
2.0. The advantage of Lambda Expressions over anonymous methods is that it is
a superset of anonymous methods (more functionality) and that it provides a more
concise, functional syntax for writing Anonymous Methods (<a href="http://download.microsoft.com/download/9/5/0/9503e33e-fde6-4aed-b5d0-ffe749822f1b/csharp%203.0%20specification.doc" target="_blank">C#
3.0 specification</a>).
</p>
        <p>
Based on the example of <a href="http://www.michelvandervlugt.net/PermaLink,guid,d0d57645-1310-4c05-9931-8be40e6e0f27.aspx" target="_blank">my
Extension Methods post</a> a foreach method, which takes a generic Action delegate
as a parameter, can be created with an anonymous method. To invoke
the SaySomething method for every Person object in the list you can write:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:70fdef49-1393-47b5-9a4c-c755bf55a48b" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #000000">_persons.ForEach(</span>
              <span style="COLOR: #0000ff">delegate</span>
              <span style="COLOR: #000000">(Person
p) { p.SaySomething(</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Hello</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">);
} );</span>
            </div>
          </pre>
        </div>
        <p>
Using the Lambda Expression syntax this same code will be:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:59480579-e659-4a42-a573-1d2f8029e9c5" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #000000">_persons.ForEach(
p </span>
              <span style="COLOR: #000000">=&gt;</span>
              <span style="COLOR: #000000"> p.SaySomething(</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Hello</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">)
); </span>
            </div>
          </pre>
        </div>
        <p>
The syntax of <em>param =&gt; expression</em> is not only shorter and easier to read
(once you are used to it), it is also implicitly typed. The type of p is inferred
by the compiler and IntelliSense as a Person object, which is possible because
the ForEach method is executed on a list of Person objects. The parameter p can also
be explicitly typed by using the syntax <em>(Person p) =&gt; p.Say...()</em>.
</p>
        <p>
In this case, where the Lambda Expression takes one parameter and doesn't have
a return value, the definition of the method is with a parameter of the generic
delegate type Action&lt;T&gt;. So in case you want to write a method, e.g. as
an IList extension, that takes these kind of Lambda Expressions you can write:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:fe4906d8-af23-4d41-a1c4-48a15c1ab9fe" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #0000ff">public</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">static</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">void</span>
              <span style="COLOR: #000000"> MyForEach</span>
              <span style="COLOR: #000000">&lt;</span>
              <span style="COLOR: #000000">T</span>
              <span style="COLOR: #000000">&gt;</span>
              <span style="COLOR: #000000">(</span>
              <span style="COLOR: #0000ff">this</span>
              <span style="COLOR: #000000"> IList</span>
              <span style="COLOR: #000000">&lt;</span>
              <span style="COLOR: #000000">T</span>
              <span style="COLOR: #000000">&gt;</span>
              <span style="COLOR: #000000"> list,
Action</span>
              <span style="COLOR: #000000">&lt;</span>
              <span style="COLOR: #000000">T</span>
              <span style="COLOR: #000000">&gt;</span>
              <span style="COLOR: #000000"> doFunc)</span>
            </div>
          </pre>
        </div>
        <p>
This method can be invoked exactly the same as the normal ForEach method, with either
a delegate or a Lambda Expression. Of course a lot of other great constructions
are possible, for example a DoIfMatch method that executes an action when there
is a match found. Note that for the match function you use the generic delegate Func&lt;T,
R&gt;, where T is the type of the input parameter and R is the type of the result:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2585dbcf-864a-4e59-81a4-9a706981a913" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #0000ff">public</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">static</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">void</span>
              <span style="COLOR: #000000"> DoIfMatch</span>
              <span style="COLOR: #000000">&lt;</span>
              <span style="COLOR: #000000">T</span>
              <span style="COLOR: #000000">&gt;</span>
              <span style="COLOR: #000000">(</span>
              <span style="COLOR: #0000ff">this</span>
              <span style="COLOR: #000000"> IList</span>
              <span style="COLOR: #000000">&lt;</span>
              <span style="COLOR: #000000">T</span>
              <span style="COLOR: #000000">&gt;</span>
              <span style="COLOR: #000000"> list,
Func</span>
              <span style="COLOR: #000000">&lt;</span>
              <span style="COLOR: #000000">T, </span>
              <span style="COLOR: #0000ff">bool</span>
              <span style="COLOR: #000000">&gt;</span>
              <span style="COLOR: #000000"> matchFunc,
Action</span>
              <span style="COLOR: #000000">&lt;</span>
              <span style="COLOR: #000000">T</span>
              <span style="COLOR: #000000">&gt;</span>
              <span style="COLOR: #000000"> doFunc)</span>
            </div>
          </pre>
        </div>
        <p>
This method can be called using:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:08ba089e-e023-462b-b453-d6b5e1ec5410" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #000000">_persons.DoIfMatch(
p </span>
              <span style="COLOR: #000000">=&gt;</span>
              <span style="COLOR: #000000"> p.Name </span>
              <span style="COLOR: #000000">==</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Person
II</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">, p </span>
              <span style="COLOR: #000000">=&gt;</span>
              <span style="COLOR: #000000"> p.SaySomething(</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Hello</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">)
);</span>
            </div>
          </pre>
        </div>
        <p>
In relation to LINQ Lambda Expressions are used in the Where-part and the OrderBy-part.
There is a distinction however in LINQ implementations based on IEnumerable and IQueryable.
The first one uses the Func&lt;T, R&gt; delegates as explained above. The latter
one however uses the Expression&lt;Func&lt;T, R&gt;&gt;, which makes evaluation of
the Lambda Expression possible. This way LINQ to SQL, for example, can add the where
part in de SQL query before it is send to the database. The Lambda Expression
that you use in both cases aren't any different though.
</p>
        <p>
The source code can be downloaded from here: <a href="http://www.michelvandervlugt.net/content/binary/LambdaExpressionsDemo.zip">LambdaExpressionsDemo.zip
(,87 KB)</a></p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=1090afda-a98a-484b-b963-a40d24eb3ab6" />
      </body>
      <title>.NET 3.5: Lambda Expressions</title>
      <guid isPermaLink="false">http://www.michelvandervlugt.net/PermaLink,guid,1090afda-a98a-484b-b963-a40d24eb3ab6.aspx</guid>
      <link>http://www.michelvandervlugt.net/PermaLink,guid,1090afda-a98a-484b-b963-a40d24eb3ab6.aspx</link>
      <pubDate>Thu, 09 Aug 2007 18:15:43 GMT</pubDate>
      <description>&lt;p&gt;
In addition to &lt;a href="http://www.michelvandervlugt.net/PermaLink,guid,d0d57645-1310-4c05-9931-8be40e6e0f27.aspx" target=_blank&gt;my
first post&lt;/a&gt; about the new .NET 3.5 language features my second&amp;nbsp;post in this
series&amp;nbsp;will be about&amp;nbsp;Lambda Expressions. Lambda Expressions are basically
a follow up to Anonymous Methods (in-line delegates), which were introduced in .NET
2.0. The advantage of Lambda Expressions over anonymous methods&amp;nbsp;is that it is
a superset of anonymous methods (more functionality) and that it&amp;nbsp;provides a more
concise, functional syntax for writing Anonymous Methods (&lt;a href="http://download.microsoft.com/download/9/5/0/9503e33e-fde6-4aed-b5d0-ffe749822f1b/csharp%203.0%20specification.doc" target=_blank&gt;C#
3.0 specification&lt;/a&gt;).
&lt;/p&gt;
&lt;p&gt;
Based on the example of &lt;a href="http://www.michelvandervlugt.net/PermaLink,guid,d0d57645-1310-4c05-9931-8be40e6e0f27.aspx" target=_blank&gt;my
Extension Methods post&lt;/a&gt; a foreach method, which takes a&amp;nbsp;generic Action delegate
as&amp;nbsp;a parameter,&amp;nbsp;can be created with an anonymous method. To&amp;nbsp;invoke
the SaySomething method for every Person object in the list you can write:
&lt;/p&gt;
&lt;div class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:70fdef49-1393-47b5-9a4c-c755bf55a48b contenteditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;pre style="BACKGROUND-COLOR: white"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="COLOR: #000000"&gt;_persons.ForEach(&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;delegate&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;(Person
p) { p.SaySomething(&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;Hello&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;);
} );&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Using the&amp;nbsp;Lambda Expression syntax this same code will be:
&lt;/p&gt;
&lt;div class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:59480579-e659-4a42-a573-1d2f8029e9c5 contenteditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;pre style="BACKGROUND-COLOR: white"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="COLOR: #000000"&gt;_persons.ForEach(
p &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; p.SaySomething(&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;Hello&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;)
); &lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The syntax of &lt;em&gt;param =&amp;gt; expression&lt;/em&gt; is not only shorter and easier to read
(once you are used to it), it is also implicitly typed. The type of p is inferred
by the compiler and IntelliSense as a Person object, which is possible&amp;nbsp;because
the ForEach method is executed on a list of Person objects. The parameter p can also
be explicitly typed by using the syntax &lt;em&gt;(Person p) =&amp;gt; p.Say...()&lt;/em&gt;.
&lt;/p&gt;
&lt;p&gt;
In this case, where the Lambda Expression&amp;nbsp;takes one parameter and doesn't have
a return value, the definition of&amp;nbsp;the method is with a parameter of the generic
delegate type&amp;nbsp;Action&amp;lt;T&amp;gt;. So in case you want to write a method, e.g. as
an IList extension,&amp;nbsp;that takes these kind of Lambda Expressions you can write:
&lt;/p&gt;
&lt;div class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:fe4906d8-af23-4d41-a1c4-48a15c1ab9fe contenteditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;pre style="BACKGROUND-COLOR: white"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; MyForEach&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;T&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;(&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;this&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; IList&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;T&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; list,
Action&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;T&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; doFunc)&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This method can be invoked exactly the same as the normal ForEach method, with either
a delegate or a Lambda Expression. Of course a lot of other great&amp;nbsp;constructions
are possible,&amp;nbsp;for example a DoIfMatch method that executes an action when there
is a&amp;nbsp;match found. Note that for the match function you use the generic delegate&amp;nbsp;Func&amp;lt;T,
R&amp;gt;,&amp;nbsp;where T is the type of the input parameter and R is the type of the result:
&lt;/p&gt;
&lt;div class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2585dbcf-864a-4e59-81a4-9a706981a913 contenteditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;pre style="BACKGROUND-COLOR: white"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; DoIfMatch&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;T&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;(&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;this&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; IList&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;T&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; list,
Func&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;T, &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;bool&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; matchFunc,
Action&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;T&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; doFunc)&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This method can be called using:
&lt;/p&gt;
&lt;div class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:08ba089e-e023-462b-b453-d6b5e1ec5410 contenteditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;pre style="BACKGROUND-COLOR: white"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="COLOR: #000000"&gt;_persons.DoIfMatch(
p &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; p.Name &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;==&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;Person
II&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;, p &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; p.SaySomething(&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;Hello&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;)
);&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In relation to LINQ Lambda Expressions are used in the Where-part and the OrderBy-part.
There is a distinction however in LINQ implementations based on IEnumerable and IQueryable.
The first one uses the Func&amp;lt;T, R&amp;gt;&amp;nbsp;delegates as explained above. The latter
one however uses the Expression&amp;lt;Func&amp;lt;T, R&amp;gt;&amp;gt;, which makes evaluation of
the Lambda Expression possible. This way LINQ to SQL, for example, can add the where
part in de SQL query&amp;nbsp;before it&amp;nbsp;is send to the database. The Lambda Expression
that you use in both cases aren't any different though.
&lt;/p&gt;
&lt;p&gt;
The source code can be downloaded from here: &lt;a href="http://www.michelvandervlugt.net/content/binary/LambdaExpressionsDemo.zip"&gt;LambdaExpressionsDemo.zip
(,87 KB)&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=1090afda-a98a-484b-b963-a40d24eb3ab6" /&gt;</description>
      <comments>http://www.michelvandervlugt.net/CommentView,guid,1090afda-a98a-484b-b963-a40d24eb3ab6.aspx</comments>
      <category>.NET 3.5</category>
    </item>
    <item>
      <trackback:ping>http://www.michelvandervlugt.net/Trackback.aspx?guid=d0d57645-1310-4c05-9931-8be40e6e0f27</trackback:ping>
      <pingback:server>http://www.michelvandervlugt.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.michelvandervlugt.net/PermaLink,guid,d0d57645-1310-4c05-9931-8be40e6e0f27.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.michelvandervlugt.net/CommentView,guid,d0d57645-1310-4c05-9931-8be40e6e0f27.aspx</wfw:comment>
      <wfw:commentRss>http://www.michelvandervlugt.net/SyndicationService.asmx/GetEntryCommentsRss?guid=d0d57645-1310-4c05-9931-8be40e6e0f27</wfw:commentRss>
      <slash:comments>9</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This is the first post in a series of xx that will be about the preparation of a presentation for
my HintTech colleagues, 'LINQ and the related new features of .NET 3.5'. Today
I start with the feature Extension Methods.
</p>
        <p>
In the <a href="http://download.microsoft.com/download/5/8/6/5868081c-68aa-40de-9a45-a3803d8134b8/csharp_3.0_specification.doc" target="_blank">C#
3.0 Specification</a> Extension Methods is defined as 'static methods that can
be invoked using instance method syntax, which make it possible to extend existing
types and constructed types with additional methods'. In a simple example of a Person
class that has a Name property, you might define an Extension Method using the following
syntax:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5debdd59-0403-43ba-a7c6-838dadd2ce58" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #0000ff">namespace</span>
              <span style="COLOR: #000000"> ExtensionMethodDemo.Domain.Extensions
{ </span>
              <span style="COLOR: #0000ff">public</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">static</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">class</span>
              <span style="COLOR: #000000"> PersonExtensions
{ </span>
              <span style="COLOR: #0000ff">public</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">static</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">void</span>
              <span style="COLOR: #000000"> SayGoodbye(</span>
              <span style="COLOR: #0000ff">this</span>
              <span style="COLOR: #000000"> Person
person) { Console.WriteLine(person.Name </span>
              <span style="COLOR: #000000">+</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000"> :
Goodbye</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">);
} } }</span>
            </div>
          </pre>
        </div>
        <p>
Note that both the class and the method are declared as static and that the (first) parameter
is defined as 'this Person person', which defines the type to which the extension
method applies. When you invoke this method 'the difficult' way:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5eecd68f-1073-401d-8762-e6a82b97660e" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #000000">ExtensionMethodDemo.Domain.Extensions.PersonExtensions.SayGoodbye(person);</span>
            </div>
          </pre>
        </div>
        <p>
there is basically nothing new to Extension Methods. Leave out the 'this' in the parameter
and you can do the same in .NET 1.0, 1.1 and 2.0. As with normal static methods, Extension
Methods also have the same limitations, ie. you only have access to the public's
of the Person class.
</p>
        <p>
One of the powers of Extension Methods is that (after you import the namespace)
the extended method appears as an instance method, so you can invoke it directly on
the instance of a class:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:897dfb20-d8c8-4d12-af29-534e87eb6a8b" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #000000">person.SayGoodbye();</span>
            </div>
          </pre>
        </div>
        <p>
It even appears in the IntelliSense, with an indication that it is an extension
to the original class. Taking into account that Extension Methods can also be applied
on all .NET Framework classes (including object, string, etc.), you can
imagine that some really powerful stuff is possible. For example you might have a
look at the <a href="http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx" target="_blank">In()
example of Scott Guthrie</a>, which is an extension method to object.
</p>
        <p>
Besides using Extension Methods on existing classes it is also possible to create <a href="http://msdn.microsoft.com/msdnmag/issues/07/06/CSharp30/#S2" target="_blank">shared
interface implementations</a>. Using this you can implement a general part of an
interface using Extension Methods, which can avoid duplicate code in classes that
implement the interface while clients have access to all the methods. 
</p>
        <p>
In the example below an interface is defined that is extended by a shared
interface implementation. Doing this the Person class only has to implement SaySomething
and clients have access to both methods SaySomething and SayHello through
the interface IPerson:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:727430b1-829f-43a5-bfcd-85af0ec78322" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #0000ff">public</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">interface</span>
              <span style="COLOR: #000000"> IPerson
{ </span>
              <span style="COLOR: #0000ff">void</span>
              <span style="COLOR: #000000"> SaySomething(</span>
              <span style="COLOR: #0000ff">string</span>
              <span style="COLOR: #000000"> text);
} </span>
              <span style="COLOR: #0000ff">namespace</span>
              <span style="COLOR: #000000"> ExtensionMethodDemo.Domain.Extensions
{ </span>
              <span style="COLOR: #0000ff">public</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">static</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">class</span>
              <span style="COLOR: #000000"> PersonExtensions
{ </span>
              <span style="COLOR: #0000ff">public</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">static</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">void</span>
              <span style="COLOR: #000000"> SayHello(</span>
              <span style="COLOR: #0000ff">this</span>
              <span style="COLOR: #000000"> IPerson
person) { person.SaySomething(</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Hello</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">);
} } }</span>
            </div>
          </pre>
        </div>
        <p>
In relation to LINQ the feature of Extension Methods is used for the extensions on
IEnumerable&lt;T&gt; and IQueryable&lt;T&gt;, for example the Where() method. The
advantage of implementing this as Extension Methods is that you only have the methods  in
scope when you use LINQ, not when you just need the interface.
</p>
        <p>
The source code can be downloaded from here: <a href="http://www.michelvandervlugt.net/content/binary/ExtensionMethodSample.zip">ExtensionMethodSample.zip
(,57 KB)</a></p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=d0d57645-1310-4c05-9931-8be40e6e0f27" />
      </body>
      <title>.NET 3.5: Extension Methods</title>
      <guid isPermaLink="false">http://www.michelvandervlugt.net/PermaLink,guid,d0d57645-1310-4c05-9931-8be40e6e0f27.aspx</guid>
      <link>http://www.michelvandervlugt.net/PermaLink,guid,d0d57645-1310-4c05-9931-8be40e6e0f27.aspx</link>
      <pubDate>Tue, 10 Jul 2007 19:47:26 GMT</pubDate>
      <description>&lt;p&gt;
This is the first post in a series of xx that will be about the preparation of a presentation&amp;nbsp;for
my HintTech colleagues,&amp;nbsp;'LINQ and the related new features of .NET 3.5'. Today
I start with the feature Extension Methods.
&lt;/p&gt;
&lt;p&gt;
In the &lt;a href="http://download.microsoft.com/download/5/8/6/5868081c-68aa-40de-9a45-a3803d8134b8/csharp_3.0_specification.doc" target=_blank&gt;C#
3.0 Specification&lt;/a&gt;&amp;nbsp;Extension Methods is defined as 'static methods that can
be invoked using instance method syntax, which make it possible to extend existing
types and constructed types with additional methods'. In a simple example of a Person
class that has a Name property, you might define an Extension Method using the following
syntax:
&lt;/p&gt;
&lt;div class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5debdd59-0403-43ba-a7c6-838dadd2ce58 contenteditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;pre style="BACKGROUND-COLOR: white"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="COLOR: #0000ff"&gt;namespace&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; ExtensionMethodDemo.Domain.Extensions
{ &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; PersonExtensions
{ &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; SayGoodbye(&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;this&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; Person
person) { Console.WriteLine(person.Name &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;+&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; :
Goodbye&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;);
} } }&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Note that both the class and the method are declared as static and that the (first)&amp;nbsp;parameter
is defined as 'this Person person', which defines the type to which the extension
method applies. When you invoke this method 'the difficult' way:
&lt;/p&gt;
&lt;div class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5eecd68f-1073-401d-8762-e6a82b97660e contenteditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;pre style="BACKGROUND-COLOR: white"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="COLOR: #000000"&gt;ExtensionMethodDemo.Domain.Extensions.PersonExtensions.SayGoodbye(person);&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
there is basically nothing new to Extension Methods. Leave out the 'this' in the parameter
and you can do the same in .NET 1.0, 1.1 and 2.0. As with normal static methods, Extension
Methods&amp;nbsp;also have the same limitations, ie. you only have access to the public's
of the Person class.
&lt;/p&gt;
&lt;p&gt;
One of the powers of Extension Methods is&amp;nbsp;that (after you import the namespace)
the extended method appears as an instance method, so you can invoke it directly on
the instance of a class:
&lt;/p&gt;
&lt;div class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:897dfb20-d8c8-4d12-af29-534e87eb6a8b contenteditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;pre style="BACKGROUND-COLOR: white"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="COLOR: #000000"&gt;person.SayGoodbye();&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
It even appears in the IntelliSense, with&amp;nbsp;an indication&amp;nbsp;that it is an extension
to the original class. Taking into account that Extension Methods can also be applied
on all&amp;nbsp;.NET Framework&amp;nbsp;classes (including object, string, etc.), you can
imagine that some really powerful stuff is possible. For example you might have a
look at the &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx" target=_blank&gt;In()
example of Scott Guthrie&lt;/a&gt;, which is an extension method to object.
&lt;/p&gt;
&lt;p&gt;
Besides using Extension Methods on existing classes it is also possible to create &lt;a href="http://msdn.microsoft.com/msdnmag/issues/07/06/CSharp30/#S2" target=_blank&gt;shared
interface implementations&lt;/a&gt;. Using this you can implement a general part of&amp;nbsp;an
interface using Extension Methods, which can avoid duplicate code in classes that
implement the interface while&amp;nbsp;clients have access to all the methods.&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
In the&amp;nbsp;example below an interface is defined&amp;nbsp;that is extended by a shared
interface implementation. Doing this the&amp;nbsp;Person class only has to implement SaySomething
and&amp;nbsp;clients have&amp;nbsp;access to&amp;nbsp;both methods SaySomething and SayHello through
the interface IPerson:
&lt;/p&gt;
&lt;div class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:727430b1-829f-43a5-bfcd-85af0ec78322 contenteditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;pre style="BACKGROUND-COLOR: white"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;interface&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; IPerson
{ &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; SaySomething(&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; text);
} &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;namespace&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; ExtensionMethodDemo.Domain.Extensions
{ &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; PersonExtensions
{ &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; &lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; SayHello(&lt;/span&gt;&lt;span style="COLOR: #0000ff"&gt;this&lt;/span&gt;&lt;span style="COLOR: #000000"&gt; IPerson
person) { person.SaySomething(&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;Hello&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;"&lt;/span&gt;&lt;span style="COLOR: #000000"&gt;);
} } }&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In relation to LINQ the feature of Extension Methods is used for the extensions on
IEnumerable&amp;lt;T&amp;gt; and IQueryable&amp;lt;T&amp;gt;, for example the Where() method. The
advantage of implementing this as Extension Methods is that you only have the methods&amp;nbsp;&amp;nbsp;in
scope when you use LINQ, not when you just need the interface.
&lt;/p&gt;
&lt;p&gt;
The source code can be downloaded from here: &lt;a href="http://www.michelvandervlugt.net/content/binary/ExtensionMethodSample.zip"&gt;ExtensionMethodSample.zip
(,57 KB)&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=d0d57645-1310-4c05-9931-8be40e6e0f27" /&gt;</description>
      <comments>http://www.michelvandervlugt.net/CommentView,guid,d0d57645-1310-4c05-9931-8be40e6e0f27.aspx</comments>
      <category>.NET 3.5</category>
    </item>
  </channel>
</rss>