In-Memory Query with C#2.0 and C#3.0
Here's some sample code from a demo I did a few months ago. It's a simple example of how much easier in-memory query and transformation are with C#3.0 and LINQ, compared with a straightforward implementation using C#2.0.
The Query
The goal is to implement this query:
"Get all the instance methods on System.String, ordered by name and grouped into overloads, each with the name of the overloaded member and the number of overloads."
For example, you can imagine that a C# editor IntelliSense implementation might need code such as this one to present a completion list on an instance of System.String.
C# 2.0 Implementation
C# 3.0 Implementation
Conclusion
The C#3.0 implementation is quite a bit shorter! But more importantly, it reads a lot like the English description of the query given above. It's also a more declarative description of the query - it says what to do, but not how to do it. These are general features of using LINQ to Objects with C#3.0. There are many cases where what would have been for loops, if statements, and assignments to intermediate collections - can now be written as queries, providing a clearer description of what the code is doing.
BTW - Which do you think is faster? Why?
