Tonight I gave a 90 minute deep dive talk on LINQ to Objects at the Northern Colorado .NET SIG. As requested, here is the slide deck which is pretty minimal and the sample code that we spent the bulk of the time reviewing. For those who didn’t attend the presentation, the sample code might be a bit of a challenge to follow. The general gist of the sample code is this. In CSharp2.0\Program2_.0.cs I review some foundational features added in .NET 2.0 that are crucial to the implementation of LINQ. Those .NET 2.0 features are:
- Generic Methods/Delegates/Interfaces
- Anonymous methods
After reviewing those features I then show my own scaled down implementation of LINQ in C# 2.0. Please be sure to look at the CSharp2.0\Pipes.cs source to see how I implemented my query operators. Then at the bottom of the Program2_0.cs file, in the Pipelines region, you can see what my LINQ query looks like. If you execute the query, you will see that it is indeed a functional implementation of my subset of LINQ. Unfortunately its appearance and usability are absolutely horrid.
Then switch over to the CSharp3.0\Program3_0.cs file. Here you will see how I apply each of the LINQ-oriented C# 3.0 features to my implementation of LINQ. Each C# 3.0 language feature incrementally improves the usability of my LINQ implementation up to the point that it is practically identical to Microsoft’s implementation of LINQ. Well, at least how they implement my small subset of LINQ without, er, any error checking.
In the query expressions region note that you can execute the C# query expression form (from c in GetPCStats(5,200) where …) and it actually works. What makes that surprising is that I haven’t included a "using System.Linq" statement in this file. The C# query expression form is still using *my implementation* of LINQ. For me that shows how the C# team at Microsoft implemented LINQ in a very nice and extensible manner.
Anyway here are the files. Enjoy!