<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">
  <title>MICHEL'S WEBLOG</title>
  <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/" />
  <link rel="self" href="http://www.michelvandervlugt.net/SyndicationService.asmx/GetAtom" />
  <icon>favicon.ico</icon>
  <updated>2008-05-21T21:06:22.9123053+02:00</updated>
  <author>
    <name>Michel van der Vlugt</name>
  </author>
  <subtitle>MICHELVANDERVLUGT.NET</subtitle>
  <id>http://www.michelvandervlugt.net/</id>
  <generator uri="http://www.dasblog.net" version="1.9.6264.0">DasBlog</generator>
  <entry>
    <title>LINQ To SQL: Exception when removing child object from Master-Detail relationship</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,15329cb0-5a2b-4c4a-b536-6d36db6cf8db.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,15329cb0-5a2b-4c4a-b536-6d36db6cf8db.aspx</id>
    <published>2008-05-21T21:06:22.9123053+02:00</published>
    <updated>2008-05-21T21:06:22.9123053+02:00</updated>
    <category term=".NET 3.5" label=".NET 3.5" scheme="http://www.michelvandervlugt.net/CategoryView,category,.NET%2B3.5.aspx" />
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Text Template Transformation Toolkit (T4)</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,6f9d9555-e20b-48d6-a7c6-bc092ab67ea1.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,6f9d9555-e20b-48d6-a7c6-bc092ab67ea1.aspx</id>
    <published>2008-04-19T17:01:48.2172201+02:00</published>
    <updated>2008-04-19T17:01:48.2172201+02:00</updated>
    <category term="VS2008" label="VS2008" scheme="http://www.michelvandervlugt.net/CategoryView,category,VS2008.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.infoq.com" target="_blank">InfoQ</a> recently posted an <a href="http://www.infoq.com/news/2008/04/T4" target="_blank">article</a> about
a very interesting feature in Visual Studio 2008, Text Template Transformation Toolkit
(also called T4). This toolkit is a standard feature of VS2008 and is all about generating
text files based on templates. This toolkit is part of the Domain Specific Language
Tools but it is certainly not limited to DSL's. One example of using T4 which I think
is quite interesting is the generation of config files for different deployment scenario's,
which can be found <a href="http://www.olegsych.com/2007/12/how-to-use-t4-to-generate-config-files/" target="_blank">here</a>.
</p>
        <p>
Useful links:
</p>
        <ul>
          <li>
            <a href="http://msdn2.microsoft.com/en-us/library/bb126445.aspx" target="_blank">MSDN:
Generating Artifacts By Using Text Templates</a>, the MSDN entry point with a list
of many links about this topic 
</li>
        </ul>
        <ul>
          <li>
            <a href="http://www.olegsych.com/2007/12/text-template-transformation-toolkit/" target="_blank">Text
Template Transformation Toolkit by Oleg Sych</a>, tutorial with a lot of examples
and links 
</li>
        </ul>
        <ul>
          <li>
            <a href="http://www.t4editor.net/" target="_blank">T4 Editor by Clarius</a>, a free
template editor which is very useful because template support in VS2008 is very limited</li>
        </ul>
        <p>
The T4 toolkit is also available for VS2005 in the VS2005 SDK v4 and the Guidance
Automation Toolkit (GAT).
</p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=6f9d9555-e20b-48d6-a7c6-bc092ab67ea1" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Deferred Execution behavior in LINQ providers</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,881d538d-c9c3-49c2-8f7a-2633f0d48839.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,881d538d-c9c3-49c2-8f7a-2633f0d48839.aspx</id>
    <published>2008-04-07T19:21:43.6582158+02:00</published>
    <updated>2008-04-07T19:21:43.6582158+02:00</updated>
    <category term=".NET 3.5" label=".NET 3.5" scheme="http://www.michelvandervlugt.net/CategoryView,category,.NET%2B3.5.aspx" />
    <category term="C#" label="C#" scheme="http://www.michelvandervlugt.net/CategoryView,category,C%23.aspx" />
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Vista Sidebar Notes disappeared</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,50f04d32-829b-4db0-8156-5c3c08f4bffd.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,50f04d32-829b-4db0-8156-5c3c08f4bffd.aspx</id>
    <published>2007-11-07T22:42:13.5173852+01:00</published>
    <updated>2007-11-07T22:49:29.4219587+01:00</updated>
    <category term="miscellaneous" label="miscellaneous" scheme="http://www.michelvandervlugt.net/CategoryView,category,miscellaneous.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Today I lost all my notes from the Notes gadget of the Vista Sidebar, which of course
contained some very important stuff.  After some research on the net it turns
out that I'm not the only one who experienced this problem...
</p>
        <p>
There is a way to get them back, but that depends on having the 'Previous Versions'
turned on in Vista. If you have you can restore the settings.ini file that is in the
following directory:
</p>
        <blockquote>
          <p>
C:\Users\<em>username</em>\AppData\Local\Microsoft\Windows Sidebar
</p>
        </blockquote>
        <p>
In this file all the settings of your active Sidebar gadgets are stored, including
your notes.
</p>
        <p>
In my case I could restore the file and after a restart of Sidebar.exe I got my notes back.
So a good file to include in your back-up if you don't have the 'Previous Versions'
option turned on.
</p>
        <a href="http://www.michelvandervlugt.net/content/binary/WindowsLiveWriter/VistaSidebarNotesdisappeared_1384F/settings.restore_1.jpg" atomicselection="true">
          <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="240" alt="settings.restore" src="http://www.michelvandervlugt.net/content/binary/WindowsLiveWriter/VistaSidebarNotesdisappeared_1384F/settings.restore_thumb_1.jpg" width="176" border="0" />
        </a>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=50f04d32-829b-4db0-8156-5c3c08f4bffd" />
      </div>
    </content>
  </entry>
  <entry>
    <title>.NET Framework source code</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,85f2509d-684f-430a-a0ab-e8eb1ce132af.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,85f2509d-684f-430a-a0ab-e8eb1ce132af.aspx</id>
    <published>2007-10-06T12:16:45.8704456+02:00</published>
    <updated>2007-10-06T12:16:45.8704456+02:00</updated>
    <category term=".NET 3.5" label=".NET 3.5" scheme="http://www.michelvandervlugt.net/CategoryView,category,.NET%2B3.5.aspx" />
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>.NET 3.5: Types, Variables and Initializers</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,b631cfe8-178b-40ac-9ce1-4b6777095d37.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,b631cfe8-178b-40ac-9ce1-4b6777095d37.aspx</id>
    <published>2007-08-15T21:58:16.6004225+02:00</published>
    <updated>2007-08-15T21:58:16.6004225+02:00</updated>
    <category term=".NET 3.5" label=".NET 3.5" scheme="http://www.michelvandervlugt.net/CategoryView,category,.NET%2B3.5.aspx" />
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>.NET 3.5: Lambda Expressions</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,1090afda-a98a-484b-b963-a40d24eb3ab6.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,1090afda-a98a-484b-b963-a40d24eb3ab6.aspx</id>
    <published>2007-08-09T20:15:43.4488209+02:00</published>
    <updated>2007-08-09T20:15:43.4488209+02:00</updated>
    <category term=".NET 3.5" label=".NET 3.5" scheme="http://www.michelvandervlugt.net/CategoryView,category,.NET%2B3.5.aspx" />
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Silverlight 1.0 RC and 1.1 Alpha Refresh</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,dcd1c9a1-c18d-4ec1-97fa-330ecce3f72b.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,dcd1c9a1-c18d-4ec1-97fa-330ecce3f72b.aspx</id>
    <published>2007-08-05T16:39:23.5907579+02:00</published>
    <updated>2007-08-05T16:39:23.5907579+02:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.michelvandervlugt.net/CategoryView,category,.NET.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <a href="http://www.michelvandervlugt.net/content/binary/WindowsLiveWriter/Silverlight1.0ReleaseCandidate_B63D/SilverlightExpired.png" atomicselection="true">
          <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="88" alt="SilverlightExpired" src="http://www.michelvandervlugt.net/content/binary/WindowsLiveWriter/Silverlight1.0ReleaseCandidate_B63D/SilverlightExpired_thumb.png" width="240" align="right" border="0" />
        </a>
        <p>
When opening my blog today I received an error message indicating that the WPF/E (Silverlight)
version installed on my system is out of date. The result of the error is that my <a href="http://www.michelvandervlugt.net/PermaLink,guid,6187afea-da04-4b89-bce2-997bc906c990.aspx" target="_blank">post
about Silverlight</a> isn't working anymore (in fact, the area of the app is
not refreshed and shows bits and pieces of other content on the page).
</p>
        <p>
The new 1.0 RC (javascript) and 1.1 Alpha Refresh (javascript and managed code) versions
can be downloaded from <a href="http://www.microsoft.com/silverlight/downloads.aspx" target="_blank">here</a>.
However, that doesn't help with my example, although the 'Get Microsoft Silverlight'
image is back. 
</p>
        <p>
The key to get it working again is to update the project with the new 'Silverlight Tools Alpha
Refresh for Visual Studio', which is for VS2008 Beta II. But than I
ran into error 'AG_E_INVALID_ARGUMENT 2210 Error' when the app tried to load my button
(which is in another assembly). To solve this problem you have to make sure that the
build action of the XAML control is set as 'Embedded Resource', which is described
in a blog post of <a href="http://blog.wekeroad.com/archive/2007/07/09/silverlight-how-to-avoid-the-dreaded-ag_e_invalid_argument-2210-error.aspx" target="_blank">Rob
Conery</a>.
</p>
        <p>
So the browser requires you to upgrade the client machine, but the software isn't
backwards compatible. I guess you shouldn't write software with beta's if you don't
want this to happen...
</p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=dcd1c9a1-c18d-4ec1-97fa-330ecce3f72b" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Visual Studio 2008 Beta 2 released</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,657f71d1-2753-4aa9-a7f0-167b1458a3bd.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,657f71d1-2753-4aa9-a7f0-167b1458a3bd.aspx</id>
    <published>2007-07-31T20:27:34.7558805+02:00</published>
    <updated>2007-07-31T20:27:34.7558805+02:00</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
The new beta of Visual Studio 2008 and the .NET Framework 3.5 is just released and
can be downloaded from <a href="http://msdn2.microsoft.com/en-us/vstudio/aa700831.aspx" target="_blank">http://msdn2.microsoft.com/en-us/vstudio/aa700831.aspx</a>.
</p>
        <p>
Most important for this release is that it includes a Go-Live license, which means
that you can officially use this version in a production environment. I don't know
exactly what other differences there are compared to beta 1 but I saw something about
Partial Methods. 
</p>
        <p>
It is downloading now, but that will take a while I'm afraid...
</p>
        <p>
BTW. Official release is planned for February 27, 2008 (also the date for SQL Server
and Windows Server 2008).
</p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=657f71d1-2753-4aa9-a7f0-167b1458a3bd" />
      </div>
    </content>
  </entry>
  <entry>
    <title>.NET 3.5: Extension Methods</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,d0d57645-1310-4c05-9931-8be40e6e0f27.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,d0d57645-1310-4c05-9931-8be40e6e0f27.aspx</id>
    <published>2007-07-10T21:47:26.8310884+02:00</published>
    <updated>2007-07-10T21:47:26.8310884+02:00</updated>
    <category term=".NET 3.5" label=".NET 3.5" scheme="http://www.michelvandervlugt.net/CategoryView,category,.NET%2B3.5.aspx" />
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
</feed>