<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">Tim Ng</title><subtitle type="html">Visual Basic</subtitle><id>http://blogs.msdn.com/timng/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/timng/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2007-08-27T21:00:27Z</updated><entry><title>F# Beta 2</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/archive/2009/10/19/f-beta-2.aspx" /><id>http://blogs.msdn.com/timng/archive/2009/10/19/f-beta-2.aspx</id><published>2009-10-19T23:09:08Z</published><updated>2009-10-19T23:09:08Z</updated><content type="html">&lt;p&gt;I am excited to say that today we have released F#beta 2 (part of the Visual Studio 2010 beta 2 release). Not only that, but we are also releasing a corresponding update to our CTP for Visual Studio 2008. Check it out at &lt;a href="http://www.fsharp.net"&gt;http://www.fsharp.net&lt;/a&gt;!&lt;/p&gt;  &lt;p&gt;The team has been hard at work on this release, and we hope you enjoy F#. We have fixed many bugs since beta 1, and have enhanced several features for you. Check out &lt;a href="http://blogs.msdn.com/dsyme/archive/2009/10/19/visual-studio-2010-beta2-with-f-is-now-available-plus-matching-f-ctp-update-for-vs2008.aspx"&gt;Don’s blog here&lt;/a&gt; for the full list. Also, Soma (the VP for DevDiv) recently &lt;a href="http://blogs.msdn.com/somasegar/archive/2009/10/09/f-in-vs2010.aspx"&gt;blogged about F#&lt;/a&gt; – you should check that out as well!&lt;/p&gt;  &lt;p&gt;Give F# a roll!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9909520" width="1" height="1"&gt;</content><author><name>TimNg</name><uri>http://blogs.msdn.com/members/TimNg.aspx</uri></author></entry><entry><title>F# Beta 1 Release</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/archive/2009/05/21/f-beta-1-release.aspx" /><id>http://blogs.msdn.com/timng/archive/2009/05/21/f-beta-1-release.aspx</id><published>2009-05-21T20:54:49Z</published><updated>2009-05-21T20:54:49Z</updated><content type="html">&lt;p&gt;F# beta 1 is now live!&lt;/p&gt;  &lt;p&gt;See &lt;a href="http://blogs.msdn.com/dsyme/archive/2009/05/20/visual-studio-2010-beta1-with-f-is-now-available-plus-matching-f-ctp-update-for-vs2008.aspx"&gt;Don&lt;/a&gt; and &lt;a href="http://lorgonblog.spaces.live.com/blog/cns!701679AD17B6D310!1327.entry"&gt;Brian’s&lt;/a&gt; blog post for lots more information.&lt;/p&gt;  &lt;p&gt;In particular, on the F# team, we have worked really hard to bring not one but two releases! We are releasing a fully integrated F# experience into Visual Studio 2010 targeting the .NETFX 4.0, as well as an update to the F# CTP for Visual Studio 2008 that targets .NETFX 2.0/3.5.&lt;/p&gt;  &lt;p&gt;We have done a lot of work over the last year, and while the beta is just a stepping stone to RTM, we hope you like what you see in the beta. The F# release notes on Don’s page just highlights some of the great work the F# team has done. We hope that F# will help make your functional programming life enjoyable and productive on the .NET platform.&lt;/p&gt;  &lt;p&gt;And as always feel free to send questions my way!&lt;/p&gt;  &lt;p&gt;Download the &lt;a href="http://www.microsoft.com/visualstudio/products/2010/default.mspx"&gt;Visual Studio 2010 Beta 1 release here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Download the &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7bb32f32-9fac-4f34-ad56-b0bda130cf00 "&gt;F# CTP addon for Visual Studio 2008 here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Learn more about F# &lt;a href="http://fsharp.net"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9634078" width="1" height="1"&gt;</content><author><name>TimNg</name><uri>http://blogs.msdn.com/members/TimNg.aspx</uri></author><category term="F#" scheme="http://blogs.msdn.com/timng/archive/tags/F_2300_/default.aspx" /></entry><entry><title>Critical Pause in Hyper V</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/archive/2009/03/11/critical-pause-in-hyper-v.aspx" /><id>http://blogs.msdn.com/timng/archive/2009/03/11/critical-pause-in-hyper-v.aspx</id><published>2009-03-12T04:31:48Z</published><updated>2009-03-12T04:31:48Z</updated><content type="html">&lt;p&gt;Just posting this because I ran into this problem today and searching Google/Live didn’t help (returned a site where you have to pay to get answers? I guess the &lt;a href="http://www.stackoverflow.com"&gt;http://www.stackoverflow.com&lt;/a&gt; model doesn’t work for them :)&lt;/p&gt;  &lt;p&gt;In any case, the problem I ran into was that when I started my Hyper V image, it would pause all the time about 30 seconds after I started it. It turns out that the issue is that if you are low on disk space on the real drive that your VHDs are in, Hyper V will pause your image. So if you see this, just clear up some space, and Hyper V should be good to go :)&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9471230" width="1" height="1"&gt;</content><author><name>TimNg</name><uri>http://blogs.msdn.com/members/TimNg.aspx</uri></author><category term="Random Technology" scheme="http://blogs.msdn.com/timng/archive/tags/Random+Technology/default.aspx" /></entry><entry><title>Happy New Year! And a small change for me</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/archive/2009/01/04/happy-new-year-and-a-small-change-for-me.aspx" /><id>http://blogs.msdn.com/timng/archive/2009/01/04/happy-new-year-and-a-small-change-for-me.aspx</id><published>2009-01-05T06:44:49Z</published><updated>2009-01-05T06:44:49Z</updated><content type="html">&lt;p&gt;Happy New Year! 2008 just flew by for me. And looking forward, there's a small change I wanted to let you guys know about.&lt;/p&gt;  &lt;p&gt;As many of you know, I've been the dev lead for the VB compiler team since Visual Studio 2008. A few months ago, I decided to try something new for a variety of reasons, and I decided to join the likes of &lt;a href="http://blogs.msdn.com/lukeh"&gt;Luke&lt;/a&gt;, &lt;a href="http://blogs.msdn.com/lucabol"&gt;Luca&lt;/a&gt;, &lt;a href="http://blogs.msdn.com/dsyme"&gt;Don&lt;/a&gt;, &lt;a href="http://blogs.msdn.com/chrsmith/default.aspx"&gt;Chris&lt;/a&gt;, &lt;a href="http://lorgonblog.spaces.live.com/blog/"&gt;Brian&lt;/a&gt;, and &lt;a href="http://blogs.msdn.com/jomo_fisher"&gt;Jomo&lt;/a&gt; on the F# team. Since September, I've been the &amp;quot;engineering&amp;quot; lead for the F# team, and I am now responsible for helping to ship F# in Visual Studio 2010. It's been a fun journey so far - working on the F# team has been awesome.&lt;/p&gt;  &lt;p&gt;As a result of this change, the coverage on my blog will shift a bit more to F# programming, especially over the next while. During the last few months, I've been learning more and more about F#, and I'll probably blog a little bit about interesting things for F# from a .NET developer's perspective that I've learned, and why F# might be interesting for you.&lt;/p&gt;  &lt;p&gt;You can download the latest F# CTP for Visual Studio 2008 &lt;a href="http://fsharp.net"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;And as always, feel free to let me know if you have any questions or comments!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9276145" width="1" height="1"&gt;</content><author><name>TimNg</name><uri>http://blogs.msdn.com/members/TimNg.aspx</uri></author><category term="F#" scheme="http://blogs.msdn.com/timng/archive/tags/F_2300_/default.aspx" /></entry><entry><title>SVC CodeCamp F# Talk</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/archive/2008/11/13/svc-codecamp-f-talk.aspx" /><id>http://blogs.msdn.com/timng/archive/2008/11/13/svc-codecamp-f-talk.aspx</id><published>2008-11-13T20:33:01Z</published><updated>2008-11-13T20:33:01Z</updated><content type="html">&lt;p&gt;Over the weekend, I had a chance to go to the &lt;a href="http://www.siliconvalley-codecamp.com"&gt;SVC codecamp&lt;/a&gt; and give a couple talks. Codecamp was fun, and it was great to meet many of you that I had a chance to talk to. Feel free to email me if you have any questions about any of the things I presented on or any of the discussions that we had!&lt;/p&gt;  &lt;p&gt;One of the talks that I did was a talk called &amp;quot;Introducing F#&amp;quot;. The talk was largely based on the &lt;a href="http://blogs.msdn.com/lucabol/archive/2008/11/03/my-f-pdc-presentation-is-online-ppt-and-demo-application.aspx"&gt;PDC talk&lt;/a&gt; that Luca gave, which you can view online (and he does a much better job explaining things then I do!). He also has a link to download the samples that, for the most part, my samples were based on. So feel free to check out his blog and check out his presentation on F#.&lt;/p&gt;  &lt;p&gt;(I will be publishing my samples for my LINQ talk soon : I need a couple more days to clean them up)&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:8951d8a7-0140-4ec4-a3d8-5be5264769ab" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/F#" rel="tag"&gt;F#&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9066778" width="1" height="1"&gt;</content><author><name>TimNg</name><uri>http://blogs.msdn.com/members/TimNg.aspx</uri></author><category term="F#" scheme="http://blogs.msdn.com/timng/archive/tags/F_2300_/default.aspx" /></entry><entry><title>Silicon Valley Code Camp</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/archive/2008/09/30/silicon-valley-code-camp.aspx" /><id>http://blogs.msdn.com/timng/archive/2008/09/30/silicon-valley-code-camp.aspx</id><published>2008-10-01T09:16:14Z</published><updated>2008-10-01T09:16:14Z</updated><content type="html">&lt;p&gt;It's been a long time since I've posted : we've been very busy working hard on the next version of Visual Basic (the next version of Visual Studio will officially be called &lt;a href="http://blogs.msdn.com/somasegar/archive/2008/09/29/what-s-next-for-visual-studio-and-net-fx.aspx"&gt;Visual Studio 2010&lt;/a&gt;). I'm really excited about the work we've done, and can't wait to share it with you.&lt;/p&gt;  &lt;p&gt;But for some immediate news : on Nov 8th and 9th, I will be doing two talks at the &lt;a href="http://www.siliconvalley-codecamp.com/"&gt;Silicon Valley Code Camp&lt;/a&gt; - if you are around, check it out! Drop by and say hi - it's free, and I hear there's free food! I will be doing two talks there:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The first talk is on &amp;quot;LINQ fundamentals&amp;quot; which will be based on some of the content that I've posted here before. To sum it up, I'll show you exactly what happens when you use a LINQ expression in your code. I'll be doing my samples in VB, but the content is applicable to any one who uses LINQ. &lt;/li&gt;    &lt;li&gt;The second talk will be &amp;quot;Introducing F#&amp;quot; where I will show off the new &lt;a href="http://msdn.microsoft.com/en-us/fsharp/default.aspx"&gt;F# programming language&lt;/a&gt; (we released a &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=61ad6924-93ad-48dc-8c67-60f7e7803d3c&amp;amp;displaylang=en"&gt;CTP&lt;/a&gt; recently). If you haven't tried it, try it out! &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;As usual, you can expect my talks to be heavily demo based, and I love taking questions and discussing things on the spot, so expect an interactive, fun time.&lt;/p&gt;  &lt;p&gt;If you're in the area and want to drop by and say hi, come on down! It should be a blast, and I'll definitely be around during the entire conference just chatting, learning, talking, and having a good time.   &lt;br /&gt;&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:714408ef-e31a-41d6-a8e0-7c0960ad1e9a" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/VisualBasic" rel="tag"&gt;VisualBasic&lt;/a&gt;,&lt;a href="http://technorati.com/tags/LINQ" rel="tag"&gt;LINQ&lt;/a&gt;,&lt;a href="http://technorati.com/tags/CodeCamp" rel="tag"&gt;CodeCamp&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SiliconValleyCodeCamp" rel="tag"&gt;SiliconValleyCodeCamp&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8971183" width="1" height="1"&gt;</content><author><name>TimNg</name><uri>http://blogs.msdn.com/members/TimNg.aspx</uri></author><category term="VisualBasic" scheme="http://blogs.msdn.com/timng/archive/tags/VisualBasic/default.aspx" /><category term="LINQ" scheme="http://blogs.msdn.com/timng/archive/tags/LINQ/default.aspx" /><category term="CodeCamp" scheme="http://blogs.msdn.com/timng/archive/tags/CodeCamp/default.aspx" /></entry><entry><title>VB expression trees - coalesce operator</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/archive/2008/03/24/vb-expression-trees-coalesce-operator.aspx" /><id>http://blogs.msdn.com/timng/archive/2008/03/24/vb-expression-trees-coalesce-operator.aspx</id><published>2008-03-25T08:37:00Z</published><updated>2008-03-25T08:37:00Z</updated><content type="html">&lt;p&gt;Hey there! In this post, I'll continue describing some of the things that are interesting about the VB compiler, especially related to expression trees and the consumption of expression trees in your LINQ provider.&lt;/p&gt;  &lt;p&gt;Again, this may not be too interesting if you aren't writing a LINQ provider, but I hope you read on anyway because I'll be discussing some of the subtle features of the language. I'm going to diverge far and wide, and I apologize up front if some of this is esoteric, but I want to give the appropriate background information.&lt;/p&gt;  &lt;h3&gt;Coalesce&lt;/h3&gt;  &lt;p&gt;One of the new operators introduced in VB is the coalesce operator. The coalesce operator looks like the ternary operator:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;REM Ternary operator&lt;/span&gt;&lt;br&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt;(a, b, c)&lt;br&gt;&lt;br&gt;&lt;span class="rem"&gt;REM Coalesce operator&lt;/span&gt;&lt;br&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt;(a, b)&lt;/pre&gt;

&lt;p&gt;The coalesce operator allows you to return the value "a" if "a" is not nothing, and if "a" is nothing, then it will return the value "b". We typically use this operator if we don't know whether "a" has a value or not, and want to use a "default value" in the case where "a" has no value.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Dim&lt;/span&gt; b = GetCityFromDatabase()&lt;br&gt;&lt;span class="kwrd"&gt;Dim&lt;/span&gt; city = &lt;span class="kwrd"&gt;if&lt;/span&gt;(b, &lt;span class="str"&gt;"Seattle"&lt;/span&gt;)&lt;/pre&gt;
&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;In this case, we get a city from a database using some API, and if there is no city in the database (the database contains a NULLABLE column and no value was stored there), then we'll use the default text "Seattle" as the city.&lt;/p&gt;

&lt;p&gt;Ok, cool - but what does this have to do with expression trees?&lt;/p&gt;

&lt;p&gt;The important thing to remember is this: in C#, the compiler generates the Coalesce operator node if the user explicit uses the coalesce operator (the ?? operator in C#). &lt;b&gt;In VB, the compiler will generate a Coalesce operator node in some cases even if the user did not explicitly use the coalesce operator (if).&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;This means that even if you don't plan to support the coalesce operator, &lt;b&gt;you still need to support it for the best VB experience possible.&lt;/b&gt;&lt;/p&gt;

&lt;h3&gt;Nullables&lt;/h3&gt;

&lt;p&gt;In order to explore this in the appropriate amount of detail, I have to diverge into VB's nullable semantics. In VB, the language design team decided to implement three-value nullable semantics rather than two-value nullable semantics (like C#).&lt;/p&gt;

&lt;p&gt;What's the difference? Here's a table for VB for x OP y, where OP is any relational operator (&amp;lt;, &amp;lt;=, =, &amp;gt;=, &amp;gt;), and the type of x and y are not boolean (VB has special semantics for these operators if x and y are boolean).&lt;/p&gt;

&lt;table border="1" cellpadding="2" cellspacing="0" width="374"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="125"&gt;x&lt;/td&gt;

      &lt;td valign="top" width="123"&gt;y&lt;/td&gt;

      &lt;td valign="top" width="124"&gt;RESULT&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="133"&gt;Not Nothing&lt;/td&gt;

      &lt;td valign="top" width="129"&gt;Not Nothing&lt;/td&gt;

      &lt;td valign="top" width="128"&gt;True/False&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="133"&gt;Not Nothing&lt;/td&gt;

      &lt;td valign="top" width="129"&gt;Nothing&lt;/td&gt;

      &lt;td valign="top" width="128"&gt;&lt;b&gt;Nothing&lt;/b&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="133"&gt;Nothing&lt;/td&gt;

      &lt;td valign="top" width="129"&gt;Not Nothing&lt;/td&gt;

      &lt;td valign="top" width="128"&gt;&lt;b&gt;Nothing&lt;/b&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="133"&gt;Nothing&lt;/td&gt;

      &lt;td valign="top" width="129"&gt;Nothing&lt;/td&gt;

      &lt;td valign="top" width="128"&gt;&lt;b&gt;Nothing&lt;/b&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;In essence, if any of the operands are Nothing, then the result is nothing. In C#, you always get a true or false answer.&lt;/p&gt;

&lt;p&gt;Let's just do a quick exercise:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;dim&lt;/span&gt; x &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;integer&lt;/span&gt; = 10&lt;br&gt;&lt;span class="kwrd"&gt;dim&lt;/span&gt; y &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;integer&lt;/span&gt; = 12&lt;br&gt;&lt;span class="kwrd"&gt;dim&lt;/span&gt; z &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;integer&lt;/span&gt;? = &lt;span class="kwrd"&gt;nothing&lt;/span&gt;&lt;br&gt;&lt;span class="kwrd"&gt;dim&lt;/span&gt; u &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;integer&lt;/span&gt;? = &lt;span class="kwrd"&gt;nothing&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span class="kwrd"&gt;dim&lt;/span&gt; b1 = x &amp;lt; y&lt;br&gt;&lt;span class="kwrd"&gt;dim&lt;/span&gt; b2 = x &amp;lt; z&lt;br&gt;&lt;span class="kwrd"&gt;dim&lt;/span&gt; b3 = z &amp;lt; x&lt;br&gt;&lt;span class="kwrd"&gt;dim&lt;/span&gt; b4 = z &amp;lt; u&lt;/pre&gt;

&lt;p&gt;Here are the results:&lt;/p&gt;

&lt;table border="0" cellpadding="2" cellspacing="0" width="400"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="400"&gt;b1 = true&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="400"&gt;b2 = nothing&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="400"&gt;b3 = nothing&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="400"&gt;b4 = nothing&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;What falls out of this is that the type returned by the logical operators is &lt;b&gt;Boolean?&lt;/b&gt;, and not &lt;b&gt;Boolean.&lt;/b&gt;&lt;/p&gt;

&lt;h3&gt;Conversions&lt;/h3&gt;

&lt;p&gt;In addition, a conversion from &lt;b&gt;Boolean?&lt;/b&gt; to &lt;b&gt;Boolean&lt;/b&gt; is a narrowing conversion. That is, if the Boolean? value is Nothing, converting it to Boolean will throw an exception.&lt;/p&gt;

&lt;p&gt;So what are the implications?&lt;/p&gt;

&lt;p&gt;If you have a method that takes a Boolean, and you pass it a Boolean? argument:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;sub&lt;/span&gt; bar(b &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;boolean&lt;/span&gt;)&lt;br&gt;&lt;span class="kwrd"&gt;end&lt;/span&gt; &lt;span class="kwrd"&gt;sub&lt;/span&gt;&lt;br&gt;&lt;br&gt;bar(x &amp;lt; z)&lt;/pre&gt;

&lt;p&gt;&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;Then if you have option strict &lt;b&gt;on&lt;/b&gt;, you will get a compiler error. But if option strict is &lt;b&gt;off&lt;/b&gt;, the compiler, as usual, applies narrowing conversions implicitly. In this particular case, you'll get a runtime exception.&lt;/p&gt;

&lt;p&gt;This may or may not surprise you; but it falls out logically from the facts discussed above. Keep this in mind, because you're going to see this pop up again later when we discuss queries.&lt;/p&gt;

&lt;h3&gt;Conditional context&lt;/h3&gt;

&lt;p&gt;Ok, here's a quick quiz. What happens in the following code, with option strict &lt;b&gt;on/off&lt;/b&gt;?&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; x &amp;lt; z &lt;span class="kwrd"&gt;then&lt;/span&gt;&lt;br&gt;&lt;span class="kwrd"&gt;end&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; &lt;/pre&gt;
&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;If you said that this code is an error when option strict is on and throws an exception when option strict is off (just like how it does in the above example with the method call to "bar"), you would be correct given what I've told you.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;However&lt;/b&gt;, VB has a special mode called the &lt;b&gt;conditional context&lt;/b&gt; mode. In certain cases, the compiler tries really hard to get a boolean out of an expression. We may do things like look for a &lt;b&gt;CBool&lt;/b&gt; cast, or look for an &lt;b&gt;IsTrue&lt;/b&gt; operator on your type. Or we may apply the &lt;b&gt;coalesce&lt;/b&gt; operator if the expression is a nullable-type result (I knew I would get back to the coalesce operator eventually :)&lt;/p&gt;

&lt;p&gt;And yes, you guessed correctly, the if statement is one of those places where we do this special work.&lt;/p&gt;

&lt;p&gt;In the above example, the compiler generates code that's equivalent to the following:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt;(x &amp;lt; z, &lt;span class="kwrd"&gt;false&lt;/span&gt;) &lt;span class="kwrd"&gt;then&lt;/span&gt;&lt;br&gt;&lt;span class="kwrd"&gt;end&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;This almost looks like a syntax error! But what you are really doing is applying the coalesce operator to the expression x &amp;lt; z, and if that expression is nothing, then use the RHS (that is, false) as the result.&lt;/p&gt;

&lt;p&gt;I hope everything so far has been clear. The compiler in spirit applies a coalesce operator in order to remove the potential nothing-ness from an expression safely, without the throwing a runtime error.&lt;/p&gt;

&lt;h3&gt;Queries&lt;/h3&gt;

&lt;p&gt;Another place where we apply the conditional context mode is in queries. In particular, the &lt;b&gt;where&lt;/b&gt; clause has this mode applied. For example:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;dim&lt;/span&gt; q = &lt;font color="#0000ff"&gt;from&lt;/font&gt; c &lt;span class="kwrd"&gt;in&lt;/span&gt; customers &lt;font color="#0000ff"&gt;where&lt;/font&gt; x &amp;lt; z &lt;span class="kwrd"&gt;select&lt;/span&gt; c&lt;/pre&gt;
&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;This case is similar to the if; the compiler generates something like the coalesce operator in order to try to get a boolean result out of x &amp;lt; z, and so, this code does not throw an exception.&lt;/p&gt;

&lt;p&gt;As a result, &lt;b&gt;when the above where clause gets converted to an expression tree, we will generate a coalesce operator node to get the same semantics as in the non-expression tree case &lt;/b&gt;(I knew we were talking about expression trees!)&lt;/p&gt;

&lt;p&gt;&lt;font color="#acacac"&gt;&lt;b&gt;Aside:&lt;/b&gt; We recognize that generating a coalesce is inefficient for LINQ to SQL because that translates to a CASE statement in SQL - essentially, this means that every row in the table needs to be run through the query : therefore, no indexes are used, and the performance of such a query will be very slow.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font color="#acacac"&gt;For the relational operators, we can optimize the coalesce away by using a different form of the relational operator, but this won't work in scenarios that don't have a relational operator (such as a scalar Boolean? or cases where you funcletize a function that returns Boolean?). We decided that being consistent and providing the full VB semantic understanding was important, because there are more scenarios for expression trees then just LINQ to SQL.&lt;/font&gt; &lt;/p&gt;

&lt;p&gt;&lt;b&gt;Update&lt;/b&gt;: I held off posting this blog long enough to report that the LINQ to SQL team fixed their query analyzer for VB and will now recognize this pattern and generate optimal SQL so that you get full index search speeds :) This fix should be available in an update coming later this year.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;The query scenario is exactly the scenario I described at the beginning of the article. This means that if you write a LINQ provider, you will almost invariably have to handle the coalesce node. This is because if your source is a database, you'll almost always have nullable columns in a database, meaning we will generate a tree with the coalesce node.&lt;/p&gt;

&lt;h3&gt;Extension methods&lt;/h3&gt;

&lt;p&gt;Before I close, I want to examine the case where you call the where operator in extension method format, rather than query format:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;dim&lt;/span&gt; q = customers.where(&lt;span class="kwrd"&gt;function&lt;/span&gt;(c) x &amp;lt; z)&lt;/pre&gt;
&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;Here's the final quiz: will this throw an exception when option strict is off? If you said &lt;b&gt;yes&lt;/b&gt;, you totally rock :) Method calls, as I described above, do not fall under the conditional context group, and thus, this is as unsafe as before, when we called bar(x &amp;lt; z).&lt;/p&gt;

&lt;p&gt;In this case, the expression tree that you get will not have a coalesce operator node, and thus, you'll get the unsafe, lifted version of the &amp;lt; operator, which results in a Boolean? result.&lt;/p&gt;

&lt;p&gt;How do you get the same semantics in the extension method version? Use the coalesce operator:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;dim&lt;/span&gt; q = customers.where(&lt;span class="kwrd"&gt;function&lt;/span&gt;(c) &lt;span class="kwrd"&gt;if&lt;/span&gt;(x &amp;lt; z, &lt;span class="kwrd"&gt;false&lt;/span&gt;))&lt;/pre&gt;

&lt;p&gt;Now you know why we chose to generate the coalesce operator in the case of the where query operator: to make the code as consistent as if you, the user, wanted to apply the safe conversion from Boolean? to Boolean, mapping Nothing to False.&lt;/p&gt;

&lt;p&gt;That is, the following query and extension method call is equivalent:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;dim&lt;/span&gt; q = &lt;font color="#0000ff"&gt;from&lt;/font&gt; c &lt;span class="kwrd"&gt;in&lt;/span&gt; customers &lt;font color="#0000ff"&gt;where&lt;/font&gt; c.id &amp;lt; 10) &lt;span class="kwrd"&gt;select&lt;/span&gt; c&lt;br&gt;&lt;span class="kwrd"&gt;dim&lt;/span&gt; q = customers.where(&lt;span class="kwrd"&gt;function&lt;/span&gt;(c) &lt;span class="kwrd"&gt;if&lt;/span&gt;(c.id &amp;lt; 10)).&lt;span class="kwrd"&gt;select&lt;/span&gt;(&lt;span class="kwrd"&gt;function&lt;/span&gt;(c) c)&lt;/pre&gt;
&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;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;I think I said a lot more about this subject then I intended to. I hope this all makes sense; there's definitely a lot of subtle things when you start to look at how various language features interact together.&lt;/p&gt;

&lt;p&gt;The bottom line is that a good LINQ provider should handle the coalesce operator, and in the VB case, it's very important to handle the coalesce operator.&lt;/p&gt;

&lt;p&gt;As always, if you have questions, feel free to send me email.&lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:10d16b95-2819-4da9-8acf-c922184a4b8a" style="margin: 0px; padding: 0px; display: inline; float: none;"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/VisualBasic" mce_href="http://technorati.com/tags/VisualBasic" rel="tag"&gt;VisualBasic&lt;/a&gt;,&lt;a href="http://technorati.com/tags/VB9" mce_href="http://technorati.com/tags/VB9" rel="tag"&gt;VB9&lt;/a&gt;,&lt;a href="http://technorati.com/tags/LINQ" mce_href="http://technorati.com/tags/LINQ" rel="tag"&gt;LINQ&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Coalesce" mce_href="http://technorati.com/tags/Coalesce" rel="tag"&gt;Coalesce&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ExpressionTrees" mce_href="http://technorati.com/tags/ExpressionTrees" rel="tag"&gt;ExpressionTrees&lt;/a&gt;&lt;/div&gt;
&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;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8335128" width="1" height="1"&gt;</content><author><name>TimNg</name><uri>http://blogs.msdn.com/members/TimNg.aspx</uri></author><category term="VisualBasic" scheme="http://blogs.msdn.com/timng/archive/tags/VisualBasic/default.aspx" /><category term="VB9" scheme="http://blogs.msdn.com/timng/archive/tags/VB9/default.aspx" /><category term="LINQ" scheme="http://blogs.msdn.com/timng/archive/tags/LINQ/default.aspx" /></entry><entry><title>Checking generic parameters for Nothing/null</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/archive/2008/03/23/checking-generic-parameters-for-nothing-null.aspx" /><id>http://blogs.msdn.com/timng/archive/2008/03/23/checking-generic-parameters-for-nothing-null.aspx</id><published>2008-03-24T06:53:00Z</published><updated>2008-03-24T06:53:00Z</updated><content type="html">&lt;p&gt;Consider the following code fragment:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Module&lt;/span&gt; Module1&lt;br&gt;    &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Foo(Of T)(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; x &lt;span class="kwrd"&gt;As&lt;/span&gt; T)&lt;br&gt;        &lt;span class="kwrd"&gt;If&lt;/span&gt; x &lt;span class="kwrd"&gt;Is&lt;/span&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt; &lt;span class="kwrd"&gt;Then&lt;/span&gt;&lt;br&gt;&lt;br&gt;        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;&lt;br&gt;    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;&lt;br&gt;    &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Main()&lt;br&gt;        Foo(10)&lt;br&gt;    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;&lt;br&gt;&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Module&lt;/span&gt;&lt;/pre&gt;
&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;The first thing you might ask is : "hey, how come the compiler allows me to check for Nothing? Isn't that a reference comparison? There is no constraint on T, so why does the compiler allow you to do this?" The simple answer is : it makes your life easier. Imagine that the compiler did not allow this : you now have to write two versions of every generic method : one that assumes the parameter can be Nothing and thus must make a null-check, and one that assumes the parameter must have a value. That really sucks.&lt;/p&gt;

&lt;p&gt;Ok, but what code does the compiler generate? Here's the bit of IL code that's interesting:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;IL_0001:  ldarg.0&lt;br&gt;IL_0002:  box        !!T&lt;br&gt;IL_0007:  ldnull&lt;br&gt;IL_0008:  ceq&lt;/pre&gt;
&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;Ah ha! The compiler treats x as a value type, and emits a box instruction. The it emits code that checks against null : since we've now boxed x, this pushes two reference types onto the stack for ceq to operate on.&lt;/p&gt;

&lt;p&gt;One way therefore to think about this, is that when a valuetype is passed to a method that takes a generic argument which is not constrained, the compiler will emit box instructions to make them reference types. Indeed, if you take a look at the number of managed objects, you'll definitely see that when you call Foo with a value type, an extra object is created on the heap (to see this, use the !dumpheap instruction in Visual Studio with SOS loaded - check &lt;a href="http://blogs.msdn.com/vancem/archive/2006/03/07/545596.aspx" mce_href="http://blogs.msdn.com/vancem/archive/2006/03/07/545596.aspx"&gt;this&lt;/a&gt; for more information). But when you call Foo with a reference type, no object is created on the heap.&lt;/p&gt;

&lt;p&gt;Why does this happen? Let's take a look at how the CLR jits the above IL.&lt;/p&gt;

&lt;p&gt;Here's the retail x86:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;00000000  push        edi  &lt;br&gt;00000001  push        esi  &lt;br&gt;00000002  mov         edi,ecx &lt;br&gt;00000004  cmp         dword ptr ds:[007795E0h],0 &lt;br&gt;0000000b  je          00000012 &lt;br&gt;0000000d  call        79826E47 &lt;br&gt;00000012  mov         ecx,79102290h &lt;br&gt;00000017  call        FFE60B1C &lt;br&gt;0000001c  mov         esi,eax &lt;br&gt;0000001e  mov         dword ptr [esi+4],edi &lt;br&gt;00000021  test        esi,esi &lt;br&gt;00000023  jne         0000003B&lt;/pre&gt;
&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;There, definitely, the first call is constructing the boxed value, and the second call is calling the managed helper to test object equivalence.&lt;/p&gt;

&lt;p&gt;Here's the version of retail x86 that's generated when Foo is called with a reference type:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;00000000  push        esi  &lt;br&gt;00000001  mov         esi,ecx &lt;br&gt;00000003  cmp         dword ptr ds:[007795E0h],0 &lt;br&gt;0000000a  je          00000011 &lt;br&gt;0000000c  call        79826D47 &lt;br&gt;00000011  test        esi,esi &lt;br&gt;00000013  jne         0000001C &lt;/pre&gt;

&lt;p&gt;See, no extra call to construct an object on the heap.&lt;/p&gt;

&lt;p&gt;Next time, I hope to post about some of the changes we are considering making for the next release.&lt;/p&gt;

&lt;p&gt;Tim&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8332943" width="1" height="1"&gt;</content><author><name>TimNg</name><uri>http://blogs.msdn.com/members/TimNg.aspx</uri></author><category term="VisualBasic" scheme="http://blogs.msdn.com/timng/archive/tags/VisualBasic/default.aspx" /><category term="VB9" scheme="http://blogs.msdn.com/timng/archive/tags/VB9/default.aspx" /></entry><entry><title>Immutability</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/archive/2008/01/15/immutability.aspx" /><id>http://blogs.msdn.com/timng/archive/2008/01/15/immutability.aspx</id><published>2008-01-16T07:48:16Z</published><updated>2008-01-16T07:48:16Z</updated><content type="html">&lt;p&gt;There has been quite a few discussions on immutability lately. &lt;a href="http://codebetter.com/blogs/patricksmacchia/archive/2008/01/13/immutable-types-understand-them-and-use-them.aspx"&gt;Patrick&lt;/a&gt; has written an excellent blog post summarizing some of the various thoughts on immutability along with a cool feature in NDepend to do immutable queries. The only thing that I would add is that immutability is definitely something to consider early on in the design process. It's much easier to start with a design that promotes immutability, rather then to try to graft immutability into an inherently imperative design.&lt;/p&gt; &lt;p&gt;Next time I will post on a specific bug that we had in the VB compiler that would have been prevented if the VB compiler implementation was designed to be more immutable then it is today.&lt;/p&gt; &lt;p&gt; &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ba16eca7-c021-4482-b48f-0f2e5922e727" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Programming" rel="tag"&gt;Programming&lt;/a&gt;, &lt;a href="http://technorati.com/tags/Immutability" rel="tag"&gt;Immutability&lt;/a&gt;&lt;/div&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7126008" width="1" height="1"&gt;</content><author><name>TimNg</name><uri>http://blogs.msdn.com/members/TimNg.aspx</uri></author><category term="Programming" scheme="http://blogs.msdn.com/timng/archive/tags/Programming/default.aspx" /></entry><entry><title>Using the .ToDictionary() Extension Method</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/archive/2007/12/12/using-the-todictionary-extension-method.aspx" /><id>http://blogs.msdn.com/timng/archive/2007/12/12/using-the-todictionary-extension-method.aspx</id><published>2007-12-13T10:38:43Z</published><updated>2007-12-13T10:38:43Z</updated><content type="html">&lt;p&gt;Last time, I showed how you can use the Cast() extension method defined on IEnumerable/IEnumerable(Of T) to do an element conversion for a sequence that implements one of the enumerable interfaces.&lt;/p&gt; &lt;p&gt;Today I want to talk about the ToDictionary extension method. This extension method lets you take a sequence that implements IEnumerable(Of T) and build a dictionary out of the elements of the sequence, allowing you to specify the key for each element. The key is the hash for the dictionary.&lt;/p&gt; &lt;p&gt;For example, consider the following code:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Class&lt;/span&gt; Car
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; id &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; manufacturer &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; color &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;

&lt;span class="kwrd"&gt;Function&lt;/span&gt; GetCars() &lt;span class="kwrd"&gt;As&lt;/span&gt; List(Of Car)

    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; ret = &lt;span class="kwrd"&gt;New&lt;/span&gt; List(Of Car)
    ret.Add(&lt;span class="kwrd"&gt;New&lt;/span&gt; Car &lt;span class="kwrd"&gt;With&lt;/span&gt; {.id = 1, .manufacturer = &lt;span class="str"&gt;"Toyota"&lt;/span&gt;, .color = &lt;span class="str"&gt;"Red"&lt;/span&gt;})
    ret.Add(&lt;span class="kwrd"&gt;New&lt;/span&gt; Car &lt;span class="kwrd"&gt;With&lt;/span&gt; {.id = 2, .manufacturer = &lt;span class="str"&gt;"Honda"&lt;/span&gt;, .color = &lt;span class="str"&gt;"Red"&lt;/span&gt;})

    &lt;span class="kwrd"&gt;Return&lt;/span&gt; ret
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;

&lt;span class="kwrd"&gt;Sub&lt;/span&gt; Main()
    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; cars = GetCars()
    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; dictionary = cars.ToDictionary(&lt;span class="kwrd"&gt;Function&lt;/span&gt;(c) c.id)
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;/pre&gt;
&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;This simple example builds a list of cars. We then get this list in Main(), and assign it to the variable "cars". Then we use type inference and assign to dictionary the value returned from the ToDictionary() extension method. This method returns a IDictionary(Of K, V), and in this case, the type argument V is inferred to be Car, and the type argument K is inferred based on the result of the lambda expression. Here, since we're returning the ID, it's going to be an integer.&lt;/p&gt;
&lt;p&gt;Thus the final type of dictionary is IDictionary(Of Integer, Car). Neat eh? Type inference is really awesome because it allows you to "just let the compiler do its work".&lt;/p&gt;
&lt;p&gt;You can then use dictionary like you would use any other IDictionary: you can retrieve elements using the () accessor, etc, etc. Play around with it, and see how powerful this extension method is.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Aside: how does the ToDictionary extension method work?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The ToDictionary method creates a new Dictionary(Of K, V) and adds elements to the dictionary, using the lambda expression you supplied to get the value of the key. Since the compiler has resolved all the type parameters at the call site (using both the hints from IEnumerable(Of T) and the lambda expression), you do not need to specify any type arguments.&lt;/p&gt;
&lt;p&gt;Since your lambda expression is used to get the key value, this implies that your lambda expression should return values that are "good hashes".&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:86b920c9-9ec6-4f1d-94ea-0d2ae18d553b" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/LINQ" rel="tag"&gt;LINQ&lt;/a&gt;, &lt;a href="http://technorati.com/tags/VB9" rel="tag"&gt;VB9&lt;/a&gt;, &lt;a href="http://technorati.com/tags/VisualBasic" rel="tag"&gt;VisualBasic&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6756660" width="1" height="1"&gt;</content><author><name>TimNg</name><uri>http://blogs.msdn.com/members/TimNg.aspx</uri></author><category term="VisualBasic" scheme="http://blogs.msdn.com/timng/archive/tags/VisualBasic/default.aspx" /><category term="VB9" scheme="http://blogs.msdn.com/timng/archive/tags/VB9/default.aspx" /><category term="LINQ" scheme="http://blogs.msdn.com/timng/archive/tags/LINQ/default.aspx" /></entry><entry><title>Using the .Cast() Extension Method</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/archive/2007/12/08/using-the-cast-extension-method.aspx" /><id>http://blogs.msdn.com/timng/archive/2007/12/08/using-the-cast-extension-method.aspx</id><published>2007-12-09T07:48:28Z</published><updated>2007-12-09T07:48:28Z</updated><content type="html">&lt;p&gt;If you take a look at the set of extension methods offered on IEnumerable (and IEnumerable(Of T)), you'll find that there are plenty of very interesting extension methods. One of them is the Cast() extension method.&lt;/p&gt; &lt;p&gt;What does this extension method do? It provides a way to convert an IEnumerable(Of T) to an IEnumerable(Of U). Even more conveniently, it allows you to convert an IEnumerable to an IEnumerable(Of T).&lt;/p&gt; &lt;p&gt;Many pre-VS2005 frameworks do not take advantage of generic collection types (since they weren't invented then). As a result, if you have the following code, it may not do what you expect:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Dim&lt;/span&gt; s = &lt;span class="kwrd"&gt;New&lt;/span&gt; ArrayList()
s.Add(&lt;span class="str"&gt;"String"&lt;/span&gt;)
s.Add(&lt;span class="str"&gt;"two"&lt;/span&gt;)
&lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; c &lt;span class="kwrd"&gt;In&lt;/span&gt; s
    Console.WriteLine(c.ToUpper)
&lt;span class="kwrd"&gt;Next&lt;/span&gt;
&lt;/pre&gt;
&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;If you compile this code with option strict on, you'll get an error saying that latebinding is not supported. Indeed, if you hover over the variable c, you'll notice that the type is object. Huh? I thought VB supported type inference?&lt;/p&gt;
&lt;p&gt;VB in fact does support type inference, but because an ArrayList is essentially an IEnumerable of type object, the compiler does not know what element type to infer. Therefore, it infers object, since you can think of ArrayList as isomorphic to IEnumerable(Of Object).&lt;/p&gt;
&lt;p&gt;You can tell the compiler that you really have an IEnumerable(Of T) by applying the Cast extension method like so:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Dim&lt;/span&gt; s = &lt;span class="kwrd"&gt;New&lt;/span&gt; ArrayList()
s.Add(&lt;span class="str"&gt;"String"&lt;/span&gt;)
s.Add(&lt;span class="str"&gt;"two"&lt;/span&gt;)
&lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; c &lt;span class="kwrd"&gt;In&lt;/span&gt; s.Cast(&lt;font color="#0000ff"&gt;Of&lt;/font&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;)
    Console.WriteLine(c.ToUpper)
&lt;span class="kwrd"&gt;Next&lt;/span&gt;
&lt;/pre&gt;
&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;And now, the compiler infers c as String, and you get your method, intellisense, etc. Sweet!&lt;/p&gt;
&lt;p&gt;Be warned though : the elements of ArrayList better be strings! Or else Cast will throw an exception.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:08ba10e4-ceb6-41fc-bd18-adbce28eec45" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/LINQ" rel="tag"&gt;LINQ&lt;/a&gt;, &lt;a href="http://technorati.com/tags/VisualBasic" rel="tag"&gt;VisualBasic&lt;/a&gt;, &lt;a href="http://technorati.com/tags/VB9" rel="tag"&gt;VB9&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6710290" width="1" height="1"&gt;</content><author><name>TimNg</name><uri>http://blogs.msdn.com/members/TimNg.aspx</uri></author><category term="VisualBasic" scheme="http://blogs.msdn.com/timng/archive/tags/VisualBasic/default.aspx" /><category term="VB9" scheme="http://blogs.msdn.com/timng/archive/tags/VB9/default.aspx" /><category term="LINQ" scheme="http://blogs.msdn.com/timng/archive/tags/LINQ/default.aspx" /></entry><entry><title>Understanding LINQ</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/archive/2007/12/05/understanding-linq.aspx" /><id>http://blogs.msdn.com/timng/archive/2007/12/05/understanding-linq.aspx</id><published>2007-12-06T04:24:54Z</published><updated>2007-12-06T04:24:54Z</updated><content type="html">&lt;p&gt;&lt;em&gt;Note : I plan to post this blog on the VB team blog eventually. However, because of the length and complexity, I'm posting this up on my blog for now to hopefully get some eyes on it so that I won't make a fool of myself on the team blog :) Please feel free to contact me if you think something is wrong, misleading, or could be improved on. Thanks!&lt;/em&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;A couple months ago, I had a chance to do a presentation at the New England Code Camp #8 where I talked about LINQ under the hood. This blog post is an attempt at reconstructing the presentation as best as I can.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; This is an advanced post on LINQ, the new VB9 features, and how the compiler does its magic to make LINQ work.&lt;/p&gt; &lt;p&gt;One day, I may try to record a screen cast of this as well if I manage to find time (and if people will find it helpful).&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The goal for us today is to understand what exactly happens when the compiler sees a LINQ query that is like this:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Dim&lt;/span&gt; q = &lt;font color="#0000ff"&gt;From&lt;/font&gt; c &lt;font color="#0000ff"&gt;I&lt;span class="kwrd"&gt;n&lt;/span&gt;&lt;/font&gt; customers &lt;font color="#0000ff"&gt;Where&lt;/font&gt; c.ID &lt;font color="#0000ff"&gt;M&lt;span class="kwrd"&gt;od&lt;/span&gt;&lt;/font&gt; 2 = 0 &lt;font color="#0000ff"&gt;S&lt;span class="kwrd"&gt;elect&lt;/span&gt;&lt;/font&gt; c.ID, c.Name&lt;/pre&gt;
&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;In order to understand what happens, we're going to start with exploring the world as it existed in VB 2005.&lt;/p&gt;
&lt;p&gt;We will attempt to create a doubly linked list from first principles (that is, without the help of existing FX types that were introduced in VB 2005, such as List(Of T)), because building this from scratch will help us understand the concepts at a deeper level.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Creating a doubly linked list&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Let's try to create the doubly linked list from the ground up, without relying on FX collection types. A doubly linked list will contain elements that have forward and backward pointers, so the following basic implementation will suffice:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Class&lt;/span&gt; DoublyLinkedListElement(Of T)
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; data &lt;span class="kwrd"&gt;As&lt;/span&gt; T)
        &lt;span class="kwrd"&gt;Me&lt;/span&gt;.m_data = data
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Private&lt;/span&gt; m_data &lt;span class="kwrd"&gt;As&lt;/span&gt; T
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;ReadOnly&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; Data() &lt;span class="kwrd"&gt;As&lt;/span&gt; T
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; m_data
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Private&lt;/span&gt; m_nextElement &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedListElement(Of T)
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; NextElement() &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedListElement(Of T)
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;Me&lt;/span&gt;.m_nextElement
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedListElement(Of T))
            &lt;span class="kwrd"&gt;Me&lt;/span&gt;.m_nextElement = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Private&lt;/span&gt; m_prevElement &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedListElement(Of T)
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; PrevElement() &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedListElement(Of T)
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; m_prevElement
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedListElement(Of T))
            m_prevElement = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;

&lt;span class="kwrd"&gt;Class&lt;/span&gt; DoublyLinkedList(Of T)
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; m_Head &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedListElement(Of T)
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; m_Tail &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedListElement(Of T)

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; InsertFirst(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; data &lt;span class="kwrd"&gt;As&lt;/span&gt; T)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; ele = &lt;span class="kwrd"&gt;New&lt;/span&gt; DoublyLinkedListElement(Of T)(data)
        ele.NextElement = m_Head

        &lt;span class="kwrd"&gt;If&lt;/span&gt; m_Head &lt;font color="#0000ff"&gt;IsNot&lt;/font&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt; &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            m_Head.PrevElement = ele
            m_Head = ele
        &lt;span class="kwrd"&gt;Else&lt;/span&gt;
            m_Head = ele
            m_Tail = ele
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;&lt;/pre&gt;&lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; InsertLast(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; data &lt;span class="kwrd"&gt;As&lt;/span&gt; T)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; ele = &lt;span class="kwrd"&gt;New&lt;/span&gt; DoublyLinkedListElement(Of T)(data)
        ele.PrevElement = m_Tail

        &lt;span class="kwrd"&gt;If&lt;/span&gt; m_Tail &lt;font color="#0000ff"&gt;IsNot&lt;/font&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt; &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            m_Tail.NextElement = ele
            m_Tail = ele
        &lt;span class="kwrd"&gt;Else&lt;/span&gt;
            m_Head = ele
            m_Tail = ele
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;
&lt;/pre&gt;
&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;(BTW the final code will be available as a ZIP file; but the intermediate steps won't be, so you'll have to follow this post through in order to understand the concepts).&lt;/p&gt;
&lt;p&gt;Take some time to understand what this code is doing; it's a very simply, feature-lacking doubly linked list. All that you can do to it is add to the beginning or the end of the list. Nothing more.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modeling some data on top of the doubly linked list&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The first thing that we'll try to do is to model some data on top of the doubly linked list. A typically "real world" model that maps to doubly linked lists well are trains. As a result, let's try to build some train models on top of the doubly linked list.&lt;/p&gt;
&lt;p&gt;Here's an example:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Class&lt;/span&gt; TrainCar
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; ID &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; Color &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; ToString() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="str"&gt;"ID: "&lt;/span&gt; &amp;amp; ID &amp;amp; vbCrLf &amp;amp; &lt;span class="str"&gt;"Color: "&lt;/span&gt; &amp;amp; Color
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;

&lt;span class="kwrd"&gt;Class&lt;/span&gt; PassengerCar : &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; TrainCar
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; Count &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; ToString() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="str"&gt;"Passenger Car"&lt;/span&gt; &amp;amp; vbCrLf &amp;amp; &lt;span class="str"&gt;"Count: "&lt;/span&gt; &amp;amp; Count &amp;amp; vbCrLf &amp;amp; &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.ToString()
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;

&lt;span class="kwrd"&gt;Class&lt;/span&gt; Cargo : &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; TrainCar
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; Company &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; ToString() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="str"&gt;"Cargo Car"&lt;/span&gt; &amp;amp; vbCrLf &amp;amp; &lt;span class="str"&gt;"Company: "&lt;/span&gt; &amp;amp; Company &amp;amp; vbCrLf &amp;amp; &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.ToString()
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;

&lt;span class="kwrd"&gt;Class&lt;/span&gt; Engine : &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; TrainCar
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; HorsePower &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; ToString() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="str"&gt;"Engine Car"&lt;/span&gt; &amp;amp; vbCrLf &amp;amp; &lt;span class="str"&gt;"Horsepower: "&lt;/span&gt; &amp;amp; HorsePower &amp;amp; vbCrLf &amp;amp; &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.ToString()
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;

&lt;span class="kwrd"&gt;Class&lt;/span&gt; Caboose : &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; TrainCar
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; ToString() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="str"&gt;"Caboose"&lt;/span&gt; &amp;amp; vbCrLf &amp;amp; &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.ToString()
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;

&lt;span class="kwrd"&gt;Class&lt;/span&gt; Train : &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; DoublyLinkedList(Of TrainCar)
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;
&lt;/pre&gt;
&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;Here, we are creating a small hierarchy of car types, along with a Train model that simply inherits from DoublyLinkedList and providing a specific parameter for the generic argument.&lt;/p&gt;
&lt;p&gt;This isn't necessarily the best way to model the train domain; it simply serves as an example for this blog post. Also, the code is "demo" code; a real production model for train cars will be much more robust.&lt;/p&gt;
&lt;p&gt;So now we have a basic working train model from which you can add cars to it, and that's about it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Populating the trains - new language syntax!&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Here's a simple function that will populate a train class with a number of train cars:&lt;/p&gt;&lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;Function&lt;/span&gt; PopulateTrains() &lt;span class="kwrd"&gt;As&lt;/span&gt; Train
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; t = &lt;span class="kwrd"&gt;New&lt;/span&gt; Train()

        t.InsertFirst(&lt;span class="kwrd"&gt;New&lt;/span&gt; Engine &lt;span class="kwrd"&gt;With&lt;/span&gt; {.ID = 1, .Color = &lt;span class="str"&gt;"Red"&lt;/span&gt;, .HorsePower = 100})
        t.InsertLast(&lt;span class="kwrd"&gt;New&lt;/span&gt; Engine &lt;span class="kwrd"&gt;With&lt;/span&gt; {.ID = 2, .Color = &lt;span class="str"&gt;"Black"&lt;/span&gt;, .HorsePower = 100})
        t.InsertLast(&lt;span class="kwrd"&gt;New&lt;/span&gt; Engine &lt;span class="kwrd"&gt;With&lt;/span&gt; {.ID = 3, .Color = &lt;span class="str"&gt;"Red"&lt;/span&gt;, .HorsePower = 75})
        t.InsertLast(&lt;span class="kwrd"&gt;New&lt;/span&gt; Engine &lt;span class="kwrd"&gt;With&lt;/span&gt; {.ID = 4, .Color = &lt;span class="str"&gt;"Green"&lt;/span&gt;, .HorsePower = 75})
        t.InsertLast(&lt;span class="kwrd"&gt;New&lt;/span&gt; Engine &lt;span class="kwrd"&gt;With&lt;/span&gt; {.ID = 5, .Color = &lt;span class="str"&gt;"Red"&lt;/span&gt;, .HorsePower = 75})
        t.InsertLast(&lt;span class="kwrd"&gt;New&lt;/span&gt; PassengerCar &lt;span class="kwrd"&gt;With&lt;/span&gt; {.ID = 6, .Color = &lt;span class="str"&gt;"Red"&lt;/span&gt;, .Count = 50})
        t.InsertLast(&lt;span class="kwrd"&gt;New&lt;/span&gt; PassengerCar &lt;span class="kwrd"&gt;With&lt;/span&gt; {.ID = 7, .Color = &lt;span class="str"&gt;"Green"&lt;/span&gt;, .Count = 51})
        t.InsertLast(&lt;span class="kwrd"&gt;New&lt;/span&gt; PassengerCar &lt;span class="kwrd"&gt;With&lt;/span&gt; {.ID = 8, .Color = &lt;span class="str"&gt;"Green"&lt;/span&gt;, .Count = 50})
        t.InsertLast(&lt;span class="kwrd"&gt;New&lt;/span&gt; Cargo &lt;span class="kwrd"&gt;With&lt;/span&gt; {.ID = 9, .Color = &lt;span class="str"&gt;"Blue"&lt;/span&gt;, .Company = &lt;span class="str"&gt;"Microsoft"&lt;/span&gt;})
        t.InsertLast(&lt;span class="kwrd"&gt;New&lt;/span&gt; Cargo &lt;span class="kwrd"&gt;With&lt;/span&gt; {.ID = 10, .Color = &lt;span class="str"&gt;"Blue"&lt;/span&gt;, .Company = &lt;span class="str"&gt;"Microsoft"&lt;/span&gt;})
        t.InsertLast(&lt;span class="kwrd"&gt;New&lt;/span&gt; Cargo &lt;span class="kwrd"&gt;With&lt;/span&gt; {.ID = 11, .Color = &lt;span class="str"&gt;"Blue"&lt;/span&gt;, .Company = &lt;span class="str"&gt;"Microsoft"&lt;/span&gt;})
        t.InsertLast(&lt;span class="kwrd"&gt;New&lt;/span&gt; PassengerCar &lt;span class="kwrd"&gt;With&lt;/span&gt; {.ID = 12, .Color = &lt;span class="str"&gt;"Black"&lt;/span&gt;, .Count = 55})
        t.InsertLast(&lt;span class="kwrd"&gt;New&lt;/span&gt; Cargo &lt;span class="kwrd"&gt;With&lt;/span&gt; {.ID = 13, .Color = &lt;span class="str"&gt;"Green"&lt;/span&gt;, .Company = &lt;span class="str"&gt;"Starbucks"&lt;/span&gt;})
        t.InsertLast(&lt;span class="kwrd"&gt;New&lt;/span&gt; Cargo &lt;span class="kwrd"&gt;With&lt;/span&gt; {.ID = 14, .Color = &lt;span class="str"&gt;"Green"&lt;/span&gt;, .Company = &lt;span class="str"&gt;"Starbucks"&lt;/span&gt;})
        t.InsertLast(&lt;span class="kwrd"&gt;New&lt;/span&gt; Caboose &lt;span class="kwrd"&gt;With&lt;/span&gt; {.ID = 15, .Color = &lt;span class="str"&gt;"Gold"&lt;/span&gt;})

        &lt;span class="kwrd"&gt;Return&lt;/span&gt; t
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;/pre&gt;
&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;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;The first thing to notice is the new object initializer syntax. This allows you to construct an instance of an object and initialize it's fields and properties in an "expression context" - that is, any place in the VB language that expects an expression, you can use an object initializer. In the example above, we used the object initializer as an argument to a function call.&lt;/p&gt;
&lt;p&gt;This is different from the with statement, which works as a statement, and not as an expression.&lt;/p&gt;
&lt;p&gt;The second thing to notice is type inference. In the PopulateTrains method, the type of t is not object, as it would be in VS2005. Rather, the compiler &lt;strong&gt;inferred&lt;/strong&gt; the type from the right hand side of the assignment statement, and t is typed as a Train.&lt;/p&gt;
&lt;p&gt;What the compiler does in this scenario is that it notices that you did not specify a type for the variable t, and so it tries its best to determine what the type is of the right hand side of the assignment, and use that type as the type of t. However, if it cannot, then it will give up and generate an error.&lt;/p&gt;
&lt;p&gt;This is especially useful in certain places where we introduced the concept of unnamed types. You'll see this later.&lt;/p&gt;
&lt;p&gt;This new feature is controlled by a new option : &lt;strong&gt;option infer&lt;/strong&gt;. By default, it is on for new projects. You can turn it off, and you'll get VS2005 behavior.&lt;/p&gt;
&lt;p&gt;Here are a couple &lt;a href="http://blogs.msdn.com/jaredpar/archive/2007/11/26/type-inference-and-ienumerable.aspx"&gt;more&lt;/a&gt; &lt;a href="http://blogs.msdn.com/timng/archive/2007/02/11/type-inference-in-visual-basic-part-1.aspx"&gt;posts&lt;/a&gt; that talk about type inference.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Enumerating the doubly linked list&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now that we have some data, we really need more support in the doubly linked list class for this data to be useful. In the .Net Framework, all collection types can be enumerated, so let's add a custom enumerator and take advantage of the fact that Visual Basic allows you to enumerate over a type that exposes a GetEnumerator method.&lt;/p&gt;
&lt;p&gt;Here's a simple and straightforward implementation of IEnumerator for our doubly linked list (the Visual Basic IDE automatically generated the stubs for me, so I just filled in where necessary):&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Class&lt;/span&gt; DoublyLinkedListEnumerator(Of T) : &lt;span class="kwrd"&gt;Implements&lt;/span&gt; IEnumerator(Of T)
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; m_First &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedListElement(Of T)
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; m_Current &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedListElement(Of T)
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; m_Started &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; elem &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedListElement(Of T))
        m_First = elem
        m_Current = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;
        m_Started = &lt;span class="kwrd"&gt;False&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;ReadOnly&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; Current() &lt;span class="kwrd"&gt;As&lt;/span&gt; T &lt;span class="kwrd"&gt;Implements&lt;/span&gt; System.Collections.Generic.IEnumerator(Of T).Current
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; m_Current.Data
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;ReadOnly&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; Current1() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Object&lt;/span&gt; &lt;span class="kwrd"&gt;Implements&lt;/span&gt; System.Collections.IEnumerator.Current
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; m_Current.Data
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; MoveNext() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt; &lt;span class="kwrd"&gt;Implements&lt;/span&gt; System.Collections.IEnumerator.MoveNext
        &lt;span class="kwrd"&gt;If&lt;/span&gt; &lt;span class="kwrd"&gt;Not&lt;/span&gt; m_Started &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            m_Started = &lt;span class="kwrd"&gt;True&lt;/span&gt;
            m_Current = m_First

            &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;(m_Current &lt;span class="kwrd"&gt;Is&lt;/span&gt; &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;, &lt;span class="kwrd"&gt;False&lt;/span&gt;, &lt;span class="kwrd"&gt;True&lt;/span&gt;)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;

        m_Current = m_Current.NextElement
        &lt;span class="kwrd"&gt;If&lt;/span&gt; m_Current IsNot &lt;span class="kwrd"&gt;Nothing&lt;/span&gt; &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;True&lt;/span&gt;
        &lt;span class="kwrd"&gt;Else&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;False&lt;/span&gt;
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Reset() &lt;span class="kwrd"&gt;Implements&lt;/span&gt; System.Collections.IEnumerator.Reset
        m_Current = &lt;span class="kwrd"&gt;Nothing&lt;/span&gt;
        m_Started = &lt;span class="kwrd"&gt;False&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Private&lt;/span&gt; disposedValue &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt; = &lt;span class="kwrd"&gt;False&lt;/span&gt;        &lt;span class="rem"&gt;' To detect redundant calls&lt;/span&gt;

    &lt;span class="rem"&gt;' IDisposable&lt;/span&gt;
    &lt;span class="kwrd"&gt;Protected&lt;/span&gt; &lt;span class="kwrd"&gt;Overridable&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Dispose(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; disposing &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;)
        &lt;span class="kwrd"&gt;If&lt;/span&gt; &lt;span class="kwrd"&gt;Not&lt;/span&gt; &lt;span class="kwrd"&gt;Me&lt;/span&gt;.disposedValue &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            &lt;span class="kwrd"&gt;If&lt;/span&gt; disposing &lt;span class="kwrd"&gt;Then&lt;/span&gt;
                &lt;span class="rem"&gt;' TODO: free other state (managed objects).&lt;/span&gt;
            &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;

            &lt;span class="rem"&gt;' TODO: free your own state (unmanaged objects).&lt;/span&gt;
            &lt;span class="rem"&gt;' TODO: set large fields to null.&lt;/span&gt;
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
        &lt;span class="kwrd"&gt;Me&lt;/span&gt;.disposedValue = &lt;span class="kwrd"&gt;True&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

&lt;span class="preproc"&gt;#Region&lt;/span&gt; &lt;span class="str"&gt;" IDisposable Support "&lt;/span&gt;
    &lt;span class="rem"&gt;' This code added by Visual Basic to correctly implement the disposable pattern.&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Dispose() &lt;span class="kwrd"&gt;Implements&lt;/span&gt; IDisposable.Dispose
        &lt;span class="rem"&gt;' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.&lt;/span&gt;
        Dispose(&lt;span class="kwrd"&gt;True&lt;/span&gt;)
        GC.SuppressFinalize(&lt;span class="kwrd"&gt;Me&lt;/span&gt;)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="preproc"&gt;#End Region&lt;/span&gt;

&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;
&lt;/pre&gt;
&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;Take a look at the class above; it walks the doubly linked list as expected every time you ask the enumerator for the next element.&lt;/p&gt;
&lt;p&gt;Now, we need to add the following method to the &lt;strong&gt;DoublyLinkedList(Of T)&lt;/strong&gt; class defined at the beginning of the article:&lt;/p&gt;&lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overridable&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; GetEnumerator() &lt;span class="kwrd"&gt;As&lt;/span&gt; IEnumerator(Of T)
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; DoublyLinkedListEnumerator(Of T)(m_Head)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; &lt;/pre&gt;
&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;We now added enumeration support to the doubly linked list, so that we can iterate the items and get the items out.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Ok so we have enough pieces to actually get a somewhat meaningful example. Here's some code that will create a train with a bunch of train cars, and then iterate and print them out:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Module&lt;/span&gt; Module1
    &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Main()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; trains = PopulateTrains()
        &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; t &lt;span class="kwrd"&gt;in&lt;/span&gt; trains
            Console.WriteLine(t.ToString)
        &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;font color="#0000ff"&gt;Module&lt;/font&gt;&lt;/pre&gt;
&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;You should see all the train cars printed to the console. So far, so good.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Searching for specific cars&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Right now, we have a bunch of data in the form of train cars, and the only thing that we can do is to iterate the data and print it out. That's not very interesting, because very quickly we will want to be able to ask questions about our data.&lt;/p&gt;
&lt;p&gt;In real life, data is really useless unless you can ask questions about it. If you had a portfolio of stocks, you want to ask questions like, "which ones are outperforming others?", or "how long have I held these stocks?". If you had a social network of friends on Facebook, you want to ask questions like, "which friends have updated their profile recently?", or "which friends have recently poked me?".&lt;/p&gt;
&lt;p&gt;If you aren't able to ask questions and filter your data, you'll quickly be overwhelmed.&lt;/p&gt;
&lt;p&gt;So let's start by asking a specific question: let's find all the train cars that are red.&lt;/p&gt;
&lt;p&gt;One of the most natural ways (and probably the one that most people would think of) is to add a new method to the Train class. Object oriented programming is all about sending a message (calling a method) on an object, and finding the right place to handle these is of paramount importance. In this scenario, the Train class is the right model.&lt;/p&gt;
&lt;p&gt;Here's some code that will do this. Notice that we take advantage of the fact that we can iterate the train cars internally:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Function&lt;/span&gt; FindRedTrainCars() &lt;span class="kwrd"&gt;As&lt;/span&gt; Train
    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; ret = &lt;span class="kwrd"&gt;New&lt;/span&gt; Train
    &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; c &lt;span class="kwrd"&gt;In&lt;/span&gt; &lt;span class="kwrd"&gt;Me&lt;/span&gt;
        &lt;span class="kwrd"&gt;If&lt;/span&gt; c.Color = &lt;span class="str"&gt;"Red"&lt;/span&gt; &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            ret.InsertLast(c)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    &lt;span class="kwrd"&gt;Return&lt;/span&gt; ret
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;font color="#0000ff"&gt;Function&lt;/font&gt;&lt;/pre&gt;
&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;This does specifically what we asked for, and nothing more. It's fairly straight forward; so far, so good.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;More, and more, and more...&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As with all things in life, things constantly change. The problem with the prior API, FindRedTrainCars, is that it's too inflexible. You can imagine that you would end up with dozens of public APIs that must be exposed with this model. The problem with this model is that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It's inflexible for change - clients cannot easily customize the behavior 
&lt;li&gt;It's expensive to test - every public API must be rigorously tested, security reviewed, scrubbed for API consistency, etc 
&lt;li&gt;It "pollutes" your design - a class with many methods that do similar things (searching) just doesn't seem like it's been well thought out 
&lt;li&gt;It's inelegant - this one is a personal preference, but where possible, I prefer for methods to be extensible through aggregation, rather than having lots of specific methods&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;It's the last point that's really a kicker for me. So let's see what we can do about it here.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The template method pattern&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As our first stab at making the train class "feel better", let's apply the template method pattern with a twist. In the official template method pattern, you define a "template" method (which has boilerplate code) and you customize with inheritance; for us, we'll customize with aggregation rather than inheritance for a variety of reasons (for me, any time where aggregation is viable I prefer it, because I don't like inheritance unless it's needed).&lt;/p&gt;
&lt;p&gt;The way we'll do aggregation is via delegates.&lt;/p&gt;
&lt;p&gt;Let's modify the find method so that it employs the template method pattern.&lt;/p&gt;
&lt;p&gt;The first order of business is to define a delegate that is suitable. We'll follow the same implementation strategy as before, so that calls for a delegate that is applied to each train car, and we'll let the delegate decide whether or not the train car fits the bill.&lt;/p&gt;
&lt;p&gt;Here's a straight forward implementation:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Delegate&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; FindFilter(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; c &lt;span class="kwrd"&gt;As&lt;/span&gt; TrainCar) &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;
&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Find(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; filter &lt;span class="kwrd"&gt;As&lt;/span&gt; FindFilter) &lt;span class="kwrd"&gt;As&lt;/span&gt; Train
    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; ret = &lt;span class="kwrd"&gt;New&lt;/span&gt; Train
    &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; c &lt;span class="kwrd"&gt;In&lt;/span&gt; &lt;span class="kwrd"&gt;Me&lt;/span&gt;
        &lt;span class="kwrd"&gt;If&lt;/span&gt; filter(c) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            ret.InsertLast(c)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    &lt;span class="kwrd"&gt;Return&lt;/span&gt; ret
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;/pre&gt;
&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;There's nothing tricky about this : we define a delegate function that filters a train car and returns true if the filter is positive, and false otherwise.&lt;/p&gt;
&lt;p&gt;Notice that with this strategy, we've accomplished a design goal - we've deferred filtering elements to the client, and in a way that doesn't require inheritance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Built-in delegate types&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;One of the small niceties we added to the .NET Framework 3.5 is the Func type. You can use the Func type as a predefined delegate type. There are several definitions that follow this pattern:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Delegate&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Func(Of R)() &lt;span class="kwrd"&gt;As&lt;/span&gt; R
&lt;span class="kwrd"&gt;Delegate&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Func(Of TArg1, R)(a &lt;span class="kwrd"&gt;As&lt;/span&gt; TArg1) &lt;span class="kwrd"&gt;As&lt;/span&gt; R&lt;/pre&gt;
&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;And so on (there are definitions for up to 5 arguments).&lt;/p&gt;
&lt;p&gt;Thus you can write the above without having to define your delegate, FindFilter, like the following:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Find(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; filter &lt;span class="kwrd"&gt;As&lt;/span&gt; Func(Of TrainCar, &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;)) &lt;span class="kwrd"&gt;As&lt;/span&gt; Train
    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; ret = &lt;span class="kwrd"&gt;New&lt;/span&gt; Train
    &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; c &lt;span class="kwrd"&gt;In&lt;/span&gt; &lt;span class="kwrd"&gt;Me&lt;/span&gt;
        &lt;span class="kwrd"&gt;If&lt;/span&gt; filter(c) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            ret.InsertLast(c)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    &lt;span class="kwrd"&gt;Return&lt;/span&gt; ret
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;/pre&gt;
&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;This is just a nice addition so that you don't have to define delegate types for the most common (less then 5 arguments) delegates.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Using the new Find method&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Ok cool, so now let's see how we can make use of this new find method. Here's the typical way you would pass a function as a delegate to Find. First, we define a function called Filter which matches the signature, and then we take the address of the function when we call the Find method:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Module&lt;/span&gt; Module1
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Filter(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; c &lt;span class="kwrd"&gt;As&lt;/span&gt; TrainCar) &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; c.Color = &lt;span class="str"&gt;"Red"&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;

    &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Main()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; trains = PopulateTrains()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; results = trains.Find(&lt;span class="kwrd"&gt;AddressOf&lt;/span&gt; Filter)
        &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; t &lt;span class="kwrd"&gt;in&lt;/span&gt; results
            Console.WriteLine(t.ToString)
        &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Module&lt;/span&gt;
&lt;/pre&gt;
&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;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;This is pretty good! We now control the filtering, and we (as the client) can do whatever filtering we want, without making requirements to the train class.&lt;/p&gt;
&lt;p&gt;There are a couple of things that are not so cool about this though:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;When defining the Filter method, we have to make sure that the Filter method signature matches the delegate signature. 
&lt;li&gt;The Filter method can be anywhere ... meaning it's not close to the usage of it. In this particular example, if the filter method was "far away" from main, you wouldn't know exactly what criteria you were using to filter the trains, unless you stuck with good naming conventions. And we know where "good" naming conventions lead ...&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;Ok, so can we do better?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lambda expressions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We definitely can do better. We'll need a new feature in VB9 called lambda expressions. Think of a lambda expression as a "inline function" that contains an expression. The following is equivalent to the above:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Module&lt;/span&gt; Module1
    &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Main()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; trains = PopulateTrains()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; results = trains.Find(&lt;span class="kwrd"&gt;Function&lt;/span&gt;(c) c.Color = &lt;span class="str"&gt;"Red"&lt;/span&gt;)
        &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; t &lt;span class="kwrd"&gt;in&lt;/span&gt; results
            Console.WriteLine(t.ToString)
        &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;font color="#0000ff"&gt;Module&lt;/font&gt;&lt;/pre&gt;
&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;Notice all the following cool things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;You do not need to create your own private method that implements the function! The compiler essentially generates the moral equivalent of "Filter" in the above example. 
&lt;li&gt;If you followed through by typing this in the IDE, you will notice the rocking Intellisense. When you typed "c.", the VB IDE presented to you rich Intellisense! This is because the compiler was able to &lt;strong&gt;infer&lt;/strong&gt; (there's type inference again) the type of c by looking at the delegate signature this time. Since the delegate said that the type of the lambda method is a TrainCar, and you asked the compiler to find the type, we did. And you got rocking Intellisense. 
&lt;li&gt;Locality - the filter is right there! No need to "go to definition" and find the filter method : it's all in the code!&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;Lambda expressions are really powerful.&lt;/p&gt;
&lt;p&gt;But it gets better.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Let the user determine what the filter criteria is&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Let's suppose that we want to allow the user to control what color to filter the trains with. If we go back to the original design with the delegates, we essentially need a way to pass data to the Filter method. Since delegates don't allow you pass data to it, there are several unpleasant workarounds:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use a global variable (boy this sucks, I didn't even want to write it down as an option) 
&lt;li&gt;You could also use a member variable, which is marginally better than a global variable. 
&lt;li&gt;You could wrap a class around your filter method.&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;The third option is the most viable, so let's see what that looks like:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Class&lt;/span&gt; ColorFilter
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; m_Color &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; color &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;)
        &lt;span class="kwrd"&gt;Me&lt;/span&gt;.m_Color = color
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Filter(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; c &lt;span class="kwrd"&gt;As&lt;/span&gt; TrainCar) &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; c.Color = m_Color
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;

&lt;span class="kwrd"&gt;Module&lt;/span&gt; Module1
    &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Main()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; trains = PopulateTrains()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; filter = &lt;span class="kwrd"&gt;new&lt;/span&gt; ColorFilter(Console.Readline())
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; results = trains.Find(&lt;span class="kwrd"&gt;AddressOf&lt;/span&gt; filter.Filter)
        &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; t &lt;span class="kwrd"&gt;in&lt;/span&gt; results
            Console.WriteLine(t.ToString)
        &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Module&lt;/span&gt;
&lt;/pre&gt;
&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;This just got a lot worst. We now need a private class just for filtering. If we wanted 100 filters, we'd need 100 private classes. This sucks. Not much better than just inheriting 100 classes from the Train class, either.&lt;/p&gt;
&lt;p&gt;It would be nice if the lambda expression feature can handle this scenario, and it most definitely can.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lambda expressions part two - lifting variables&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Let's consider the following example, which is slightly modified from the lambda expression example above. Pay particular attention to the body of the lambda expression:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Module&lt;/span&gt; Module1
    &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Main()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; trains = PopulateTrains()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; color = Console.Readline()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; results = trains.Find(&lt;span class="kwrd"&gt;Function&lt;/span&gt;(c) c.Color = color)
        &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; t &lt;span class="kwrd"&gt;in&lt;/span&gt; results
            Console.WriteLine(t.ToString)
        &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;font color="#0000ff"&gt;Module&lt;/font&gt;&lt;/pre&gt;
&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;Notice that there's a subtlety here : the lambda expression is referring to a variable, color, which is not a lambda parameter, but is defined in Main. When this happens, the compiler generates code that is morally equivalent to the ColorFilter class, except we call the ColorFilter class a closure.&lt;/p&gt;
&lt;p&gt;That is, the two examples are equivalent. The compiler provides powerful variable lifting services for you so that you can write code that refers to variables that are not in the scope of the lambda expression, and the compiler will "make it work".&lt;/p&gt;
&lt;p&gt;You've seen the magic (we wrote the code in the ColorFilter class example!) - now you know what the compiler does.&lt;/p&gt;
&lt;p&gt;Lambda expressions are extremely powerful : check out my &lt;a href="http://msdn.microsoft.com/msdnmag/issues/07/09/BasicInstincts/"&gt;Lambda expression article&lt;/a&gt; on MSDN magazine for more coverage.&lt;/p&gt;
&lt;p&gt;Now, we really have a clean, good solution. I like what we have. But as usual, let's push on, and see what else we can do to make this even better.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Can we make it more general?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Everything we've done so far has been pretty cool, and I feel that we are converging on a good design. But let's continue to push further : we added a very general find method to the train class, but it feels like it's in the wrong place, since the act of finding elements is not necessarily restricted to finding train cars in trains.&lt;/p&gt;
&lt;p&gt;We really want to add this method to the DoublyLinkedList class, so that the train class just gains the benefit of the find method, as well as any other class that uses the DoublyLinkedList class.&lt;/p&gt;
&lt;p&gt;But what if we've already shipped the DoublyLinkedList class, and we can't modify the interface anymore? Or what if the DoublyLinkedList class belong to someone else (and we purchased the rights to the API library, for example)?&lt;/p&gt;
&lt;p&gt;We need a way to extend the behavior of an existing class.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Extension methods&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;One of the most trivial ways we can "extend" the behavior of an existing class is to create a shared method that takes this class as the first parameter (sort of like the ADT way of doing things way before OOP became popular). Here's some code that will do just that:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Module&lt;/span&gt; DoublyLinkedListExtensions
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Find(Of TElement)(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; col &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedList(Of TElement), _&lt;br&gt;                                      &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; filter &lt;span class="kwrd"&gt;As&lt;/span&gt; Func(Of TElement, &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;)) &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedList(Of TElement)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; ret = &lt;span class="kwrd"&gt;New&lt;/span&gt; DoublyLinkedList(Of TElement)
        &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; c &lt;span class="kwrd"&gt;In&lt;/span&gt; col
            &lt;span class="kwrd"&gt;If&lt;/span&gt; filter(c) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
                ret.InsertLast(c)
            &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
        &lt;span class="kwrd"&gt;Next&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; ret
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Module&lt;/span&gt;
&lt;/pre&gt;
&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;The implementation is pretty straight forward and is very similar to what you would do if you were to implement this function as an instance method (just like above).&lt;/p&gt;
&lt;p&gt;You use this method like so:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Module&lt;/span&gt; Module1
    &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Main()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; t = PopulateTrains()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; results = Find(t, &lt;span class="kwrd"&gt;Function&lt;/span&gt;(c) c.Color = &lt;span class="str"&gt;"Red"&lt;/span&gt;)
        &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; train &lt;span class="kwrd"&gt;in&lt;/span&gt; results
            Console.WriteLine(train.ToString)
        &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;font color="#0000ff"&gt;Module&lt;/font&gt;&lt;/pre&gt;
&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;This is how you would call the shared method. There are a couple of things with the shared method approach that are a bit lacking:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We really want Find to be like an instance method on DoublyLinkedList. The shared method syntax obfuscates this. 
&lt;li&gt;In this particular case, the type argument TElement is fixed by the element type for DoublyLinkedList, but there's no way the compiler knows this. We really want to tell the compiler that TElement should be inferred based on only the DoublyLinkedList's argument.&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;For example, here's what the tooltip and intellisense show:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/timng/WindowsLiveWriter/LINQunderthehood_BCDC/shared_method_1.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="130" alt="shared_method" src="http://blogs.msdn.com/blogfiles/timng/WindowsLiveWriter/LINQunderthehood_BCDC/shared_method_thumb_1.jpg" width="913" border="0"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Notice that in this case, we have no idea what TElement is. As a result, when you are writing the lambda expression, you don't get intellisense:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/timng/WindowsLiveWriter/LINQunderthehood_BCDC/shared_method_2.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="128" alt="shared_method_2" src="http://blogs.msdn.com/blogfiles/timng/WindowsLiveWriter/LINQunderthehood_BCDC/shared_method_2_thumb.jpg" width="448" border="0"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;So while shared methods work, they don't really give the greatest user experience.&lt;/p&gt;
&lt;p&gt;This is where extension methods come in. One of my team mates, &lt;a href="http://blogs.msdn.com/vbteam/archive/tags/Scott+Wisniewski/default.aspx"&gt;Scott&lt;/a&gt;, has a series of articles on extension methods that are extremely well written and in-depth : I encourage you to check those out if you aren't too familiar with extension methods.&lt;/p&gt;
&lt;p&gt;What I want to highlight is that extension methods have two very nice properties:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;You can "see" extension methods as if they were instance methods. 
&lt;li&gt;A feature called partial type inference makes the intellisense scenarios amazing.&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;As above, I'll demonstrate with some samples. To make your shared method an extension method, simply supply the &amp;lt;Extension&amp;gt; attribute : and that's it! Nothing more. It's really easy to do:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Module&lt;/span&gt; DoublyLinkedListExtensions
    &amp;lt;Extension&amp;gt; _
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Find(Of TElement)(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; col &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedList(Of TElement), _&lt;br&gt;                         &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; filter &lt;span class="kwrd"&gt;As&lt;/span&gt; Func(Of TElement, &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;)) &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedList(Of TElement)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; ret = &lt;span class="kwrd"&gt;New&lt;/span&gt; DoublyLinkedList(Of TElement)
        &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; c &lt;span class="kwrd"&gt;In&lt;/span&gt; col
            &lt;span class="kwrd"&gt;If&lt;/span&gt; filter(c) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
                ret.InsertLast(c)
            &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
        &lt;span class="kwrd"&gt;Next&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; ret
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Module&lt;/span&gt;
&lt;/pre&gt;
&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;Be sure to import the System.Runtime.CompilerServices namespace.&lt;/p&gt;
&lt;p&gt;Now, you can write code that looks like this:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Module&lt;/span&gt; Module1
    &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Main()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; t = PopulateTrains()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; results = t.Find(&lt;span class="kwrd"&gt;Function&lt;/span&gt;(c) c.Color = &lt;span class="str"&gt;"Red"&lt;/span&gt;)
        &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; t &lt;span class="kwrd"&gt;in&lt;/span&gt; results
            Console.WriteLine(t.ToString)
        &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;font color="#0000ff"&gt;Module&lt;/font&gt;&lt;/pre&gt;
&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;As you can see, you now call the Find method as if it were an instance method : in a sense, we've "extended" the type DoublyLinkedList with a new method called Find.&lt;/p&gt;
&lt;p&gt;But the power really comes from the user experience. Check out the intellisense:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/timng/WindowsLiveWriter/LINQunderthehood_BCDC/extension_method.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="147" alt="extension_method" src="http://blogs.msdn.com/blogfiles/timng/WindowsLiveWriter/LINQunderthehood_BCDC/extension_method_thumb.jpg" width="811" border="0"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Check that out! The partial type inference feature essentially performs type inference on TElement based on the "instance" type : in this case, it decided that TElement is a TrainCar, and now, you've got better tooltips!&lt;/p&gt;
&lt;p&gt;And not only that, when you write your lambda expression, we now know what the lambda argument is, so you get intellisense there as well:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/timng/WindowsLiveWriter/LINQunderthehood_BCDC/extension_method_2.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="156" alt="extension_method_2" src="http://blogs.msdn.com/blogfiles/timng/WindowsLiveWriter/LINQunderthehood_BCDC/extension_method_2_thumb.jpg" width="407" border="0"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Look at that! We now know that x is a TrainCar, so you get intellisense there!&lt;/p&gt;
&lt;p&gt;This is the power of extension methods. Again, please check out Scott's articles for more details.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Projections&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To hammer home the idea of extension methods, I want to define another extension method as an example. This method transforms a DoublyLinkedList of type T to a DoublyLinkedList of type R, like so:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&amp;lt;Extension()&amp;gt; _
&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Transform(Of TSource, TResult)(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; col &lt;span class="kwrd"&gt;As&lt;/span&gt; DoubleLinkedList(Of TSource), _&lt;br&gt;                          &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; Projection &lt;span class="kwrd"&gt;As&lt;/span&gt; Func(Of TSource, TResult)) &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedList(Of TResult)
    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; ret = &lt;span class="kwrd"&gt;New&lt;/span&gt; DoublyLinkedList(Of TResult)
    &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; c &lt;span class="kwrd"&gt;In&lt;/span&gt; col
        ret.InsertLast(Projection(c))
    &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    &lt;span class="kwrd"&gt;Return&lt;/span&gt; ret
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;&lt;/pre&gt;
&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;This is a simple extension method that just converts elements in a list. Check it out yourself and make sure you understand the concepts, and try to call it and see what results you get.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Where are we?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;So now we have a very good design. We can choose to use instance methods or extension methods to provide a template method pattern of access for our clients, and our clients can write clear, concise code to use our collection.&lt;/p&gt;
&lt;p&gt;But we can do more. I have not written at all about LINQ yet, and we're now going to see how LINQ works.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Using LINQ on our DoublyLinkedList class&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The wishful thinking part of me wishes that the following code would compile, but it does not:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Module&lt;/span&gt; Module1
    &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Main()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; trains = PopulateTrains()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; q = &lt;font color="#0000ff"&gt;From&lt;/font&gt; t &lt;span class="kwrd"&gt;In&lt;/span&gt; trains _
                &lt;font color="#0000ff"&gt;Where&lt;/font&gt; t.Color = &lt;span class="str"&gt;"Red"&lt;/span&gt; _
                &lt;span class="kwrd"&gt;Select&lt;/span&gt; t
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Module&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;We really want to make this work, because this would be the ultimate syntax for what we want. It would allow us to ask information about our train classes (and any class that inherits from our doublylinkedlist class) through the new LINQ operators.&lt;/p&gt;
&lt;p&gt;Are you ready to see this work? Simply change the method "Find" to "Where" and "Transform" to "Select":&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Module&lt;/span&gt; DoublyLinkedListExtensions
    &amp;lt;Extension()&amp;gt; _
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Where(Of TElement)(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; col &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedList(Of TElement), _&lt;br&gt;                          &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; filter &lt;span class="kwrd"&gt;As&lt;/span&gt; Func(Of TElement, &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;)) &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedList(Of TElement)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; ret = &lt;span class="kwrd"&gt;New&lt;/span&gt; DoublyLinkedList(Of TElement)
        &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; c &lt;span class="kwrd"&gt;In&lt;/span&gt; col
            &lt;span class="kwrd"&gt;If&lt;/span&gt; filter(c) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
                ret.InsertLast(c)
            &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
        &lt;span class="kwrd"&gt;Next&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; ret
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;

    &amp;lt;Extension()&amp;gt; _
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; [&lt;span class="kwrd"&gt;Select&lt;/span&gt;](Of TSource, TResult)(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; col &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedList(Of TSource), _&lt;br&gt;                             &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; Projection &lt;span class="kwrd"&gt;As&lt;/span&gt; Func(Of TSource, TResult)) &lt;span class="kwrd"&gt;As&lt;/span&gt; DoublyLinkedList(Of TResult)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; ret = &lt;span class="kwrd"&gt;New&lt;/span&gt; DoublyLinkedList(Of TResult)
        &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; c &lt;span class="kwrd"&gt;In&lt;/span&gt; col
            ret.InsertLast(Projection(c))
        &lt;span class="kwrd"&gt;Next&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; ret
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Module&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the code compiles! We can use LINQ over our DoublyLinkedList!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Wrapping up&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Whew. I had originally hoped to have this post done a long time ago. And the initial draft ideas had this post at about a quarter of the length. If you're still reading, I hope you've gotten a very good idea of what exactly the compiler does when it sees a LINQ statement.&lt;/p&gt;
&lt;p&gt;After all, the whole point of this post is to understand what magic the compiler performs, in order for you to have the knowledge and know-how of how LINQ works.&lt;/p&gt;
&lt;p&gt;To summarize in reverse order:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We've seen how the compiler takes a LINQ query and decomposes it into method calls (extension methods or instance methods). 
&lt;li&gt;We've seen how the compiler takes the expressions in a LINQ query and builds lambda expressions out of them. 
&lt;li&gt;We've seen how the compiler takes lambda expressions and creates closure classes and methods that have the expression body, and replaces the call site with the address of the compiler generated method. 
&lt;li&gt;We've seen that the pattern LINQ employs is a template pattern with customization via delegates, and how that is a better way to write code then customization via inheritance.&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;I hope that you can move forward and backwards : that is, that not only do you understand how to decompose a LINQ query into its parts (for those rare cases where you need to call a method that's not available in query format), but you also understand how to take advantage of LINQ in your legacy applications, by following a sequence of refactorings similar to the one I did in this article.&lt;/p&gt;
&lt;p&gt;And there's still much more to say about LINQ:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Anonymous types 
&lt;li&gt;Deferred execution 
&lt;li&gt;Expression trees 
&lt;li&gt;Other complex operators, like groupby and join&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;I'll save these for a post in the future.&lt;/p&gt;
&lt;p&gt;As always, if you have questions, feel free to email me or leave a comment!&lt;/p&gt;
&lt;p&gt;Thanks, and I hope you enjoy Visual Basic 2008! On the product team, we love this product, we love LINQ, and we hope that you find it as enjoyable, productive, and useful as we do.&lt;/p&gt;
&lt;p&gt;Tim&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:c1c7d60c-5556-4cfc-acb5-bcca98bdc28d" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/LINQ" rel="tag"&gt;LINQ&lt;/a&gt;, &lt;a href="http://technorati.com/tags/VB9" rel="tag"&gt;VB9&lt;/a&gt;, &lt;a href="http://technorati.com/tags/VisualBasic" rel="tag"&gt;VisualBasic&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6673007" width="1" height="1"&gt;</content><author><name>TimNg</name><uri>http://blogs.msdn.com/members/TimNg.aspx</uri></author><category term="VisualBasic" scheme="http://blogs.msdn.com/timng/archive/tags/VisualBasic/default.aspx" /><category term="VB9" scheme="http://blogs.msdn.com/timng/archive/tags/VB9/default.aspx" /><category term="LINQ" scheme="http://blogs.msdn.com/timng/archive/tags/LINQ/default.aspx" /></entry><entry><title>Creating a class that has an anonymous type as a field</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/archive/2007/11/06/creating-a-class-that-has-an-anonymous-type-as-a-field.aspx" /><id>http://blogs.msdn.com/timng/archive/2007/11/06/creating-a-class-that-has-an-anonymous-type-as-a-field.aspx</id><published>2007-11-07T10:25:00Z</published><updated>2007-11-07T10:25:00Z</updated><content type="html">
&lt;p&gt;Here's another way to use the generic parameter trick to get an anonymous type as a field. The other day, I was trying to store the result of a lambda expression in a field, but the lambda was typed as Func(Of &amp;lt;anonymoustype&amp;gt;) and since there is no type inference for fields, and I didn't want to use object (which would be perfectly fine normally, but in this case I wanted a typed anonymous type), I used a similar trick to a post I did a while ago on creating collections of anonymous types.&lt;/p&gt;
 
&lt;p&gt;However, this is not as straightforward because we cannot rely on type inference in the constructor. What I would have liked to be able to do is something like this:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Test(&lt;font color="#0000ff"&gt;Of&lt;/font&gt; T)&lt;br&gt;    &lt;span class="kwrd"&gt;Private&lt;/span&gt; x &lt;span class="kwrd"&gt;As&lt;/span&gt; Func(&lt;font color="#0000ff"&gt;Of&lt;/font&gt; T)&lt;br&gt;    &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(y &lt;span class="kwrd"&gt;As&lt;/span&gt; T)&lt;br&gt;    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;&lt;br&gt;&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span class="kwrd"&gt;Sub&lt;/span&gt; UseTest()&lt;br&gt;    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; x = &lt;span class="kwrd"&gt;new&lt;/span&gt; Test(&lt;span class="kwrd"&gt;New&lt;/span&gt; &lt;span class="kwrd"&gt;With&lt;/span&gt; {.x = 10, .y = &lt;span class="str"&gt;"Tim"&lt;/span&gt;})&lt;br&gt;&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&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;However, this doesn't quite work because we currently don't allow type inference on constructor calls; that is, you have to explicitly state the type parameter for T when you new an object.&lt;/p&gt;

&lt;p&gt;Ok, so let's try another technique, which involves using a factory method:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; TestFactory&lt;br&gt;    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; CreateTest(&lt;font color="#0000ff"&gt;Of&lt;/font&gt; T)(x &lt;span class="kwrd"&gt;As&lt;/span&gt; T) &lt;span class="kwrd"&gt;As&lt;/span&gt; Test(Of T)&lt;br&gt;        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Test(Of T)&lt;br&gt;    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;&lt;br&gt;&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Test(&lt;font color="#0000ff"&gt;Of&lt;/font&gt; T)&lt;br&gt;    &lt;span class="kwrd"&gt;Private&lt;/span&gt; x &lt;span class="kwrd"&gt;As&lt;/span&gt; Func(&lt;font color="#0000ff"&gt;Of&lt;/font&gt; T)&lt;br&gt;&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span class="kwrd"&gt;Sub&lt;/span&gt; UseTest()&lt;br&gt;    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; x = TestFactory.CreateTest(&lt;span class="kwrd"&gt;New&lt;/span&gt; &lt;span class="kwrd"&gt;With&lt;/span&gt; {.x = 10, .y = &lt;span class="str"&gt;"Tim"&lt;/span&gt;})&lt;br&gt;&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&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;Ok, this works now, and you get a Test(Of &amp;lt;anonymoustype&amp;gt;) which has a field that's Func(Of &amp;lt;anonymoustype&amp;gt;), but we had to jump through hoops and use some unnatural techniques to get this.&lt;/p&gt;

&lt;p&gt;In fact, since the main benefit of anonymous types is to avoid creating types, but in this case we have to create a factory class in order to get to this "hack", we didn't really gain anything.&lt;/p&gt;

&lt;p&gt;This definitely belongs to the category "interesting, but I would never use this unless I wanted to confuse people". But it really shows how powerful type inference is for generic parameters.&lt;/p&gt;

&lt;p&gt;However, I'm sure someone will find a use for this.&lt;/p&gt;

&lt;p&gt;Anyways, I'll try to stop beating this horse :)&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="0767317B-992E-4b12-91E0-4F059A8CECA8:866734b1-3e94-4431-9a0c-d75ecb401837" contenteditable="false" style="margin: 0px; padding: 0px; display: inline;"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/VisualBasic" mce_href="http://technorati.com/tags/VisualBasic" rel="tag"&gt;VisualBasic&lt;/a&gt;, &lt;a href="http://technorati.com/tags/VB9" mce_href="http://technorati.com/tags/VB9" rel="tag"&gt;VB9&lt;/a&gt;, &lt;a href="http://technorati.com/tags/AnonymousTypes" mce_href="http://technorati.com/tags/AnonymousTypes" rel="tag"&gt;AnonymousTypes&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5952805" width="1" height="1"&gt;</content><author><name>TimNg</name><uri>http://blogs.msdn.com/members/TimNg.aspx</uri></author><category term="VB9" scheme="http://blogs.msdn.com/timng/archive/tags/VB9/default.aspx" /></entry><entry><title>New England Code Camp</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/archive/2007/09/25/new-england-code-camp.aspx" /><id>http://blogs.msdn.com/timng/archive/2007/09/25/new-england-code-camp.aspx</id><published>2007-09-25T10:03:51Z</published><updated>2007-09-25T10:03:51Z</updated><content type="html">&lt;p&gt;It's official - I'll be going to the New England Code Camp this weekend and presenting two talks on LINQ features and Visual Basic. I hope that it will be a blast, and if you're around the New England area, I hope you drop by!&lt;/p&gt; &lt;p&gt;The two talks I have planned are "LINQ Fundamentals" (all about understanding the core concepts of LINQ and how LINQ queries translate into language features) and "LINQ-ing your data" (all about understanding how LINQ changes the way you view your data, with particular focus on LINQ to XML and XML features in VB).&lt;/p&gt; &lt;p&gt;I'll be hanging around before/after the talks, so if you just wanna stop by and talk VB, LINQ, or compiler stuff in general, that'll be cool too! It looks like there will be a bunch of good talks at this code camp as well, so definitely try to stop by if you're in the area.&lt;/p&gt; &lt;p&gt;After the talks, I'll post information and samples/slide decks on my blog (or the VB team blog, which ever is more appropriate).&lt;/p&gt; &lt;p&gt;Hope to see you there!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5114031" width="1" height="1"&gt;</content><author><name>TimNg</name><uri>http://blogs.msdn.com/members/TimNg.aspx</uri></author></entry><entry><title>Lambda expressions article published on MSDN Magazine</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/timng/archive/2007/08/27/lambda-expressions-article-published-on-msdn-magazine.aspx" /><id>http://blogs.msdn.com/timng/archive/2007/08/27/lambda-expressions-article-published-on-msdn-magazine.aspx</id><published>2007-08-28T07:00:27Z</published><updated>2007-08-28T07:00:27Z</updated><content type="html">&lt;p&gt;Hey guys, just a quick note to let you know that my &lt;a href="http://msdn.microsoft.com/msdnmag/issues/07/09/BasicInstincts/"&gt;article on lambda expressions&lt;/a&gt; has been published on MSDN magazine. I'll be updating this blog (actually, probably the &lt;a href="http://blogs.msdn.com/vbteam"&gt;VB team blog&lt;/a&gt;) with more information on lambda expressions that I didn't have time to get into the article.&lt;/p&gt; &lt;p&gt;Hope you enjoy!&lt;/p&gt; &lt;p&gt;In the mean time, our team has been busy wrapping up Orcas, fixing the remaining bugs, and otherwise getting ready to ship Orcas. I hope to have some time to discuss later the kind of changes we are exploring for the compiler in general, life as a compiler lead, and other interesting things that come up.&lt;/p&gt; &lt;div class="wlWriterSmartContent" id="0767317B-992E-4b12-91E0-4F059A8CECA8:f956175f-7b98-4437-8247-b75830c0a190" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/VB9" rel="tag"&gt;VB9&lt;/a&gt;, &lt;a href="http://technorati.com/tags/Lambda%20Expressions" rel="tag"&gt;Lambda Expressions&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4603023" width="1" height="1"&gt;</content><author><name>TimNg</name><uri>http://blogs.msdn.com/members/TimNg.aspx</uri></author><category term="VB9" scheme="http://blogs.msdn.com/timng/archive/tags/VB9/default.aspx" /><category term="LINQ" scheme="http://blogs.msdn.com/timng/archive/tags/LINQ/default.aspx" /></entry></feed>