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

 MethodInfo[] methods = typeof(string).GetMethods();

List<MethodInfo> instanceMethods = new List<MethodInfo>();
foreach (MethodInfo method in methods)
    if (!method.IsStatic)
        instanceMethods.Add(method);
instanceMethods.Sort(delegate(MethodInfo m1, MethodInfo m2) { return m1.Name.CompareTo(m2.Name); });
Dictionary<string, List<MethodInfo>> methodGroups = new Dictionary<string, List<MethodInfo>>();
foreach (MethodInfo method in instanceMethods)
{
    if (methodGroups.ContainsKey(method.Name))
    {
        methodGroups[method.Name].Add(method);
    }
    else
    {
        methodGroups.Add(method.Name, new List<MethodInfo>());
        methodGroups[method.Name].Add(method);
    }
}
List<Container> result = new List<Container>();
foreach (string name in methodGroups.Keys)
{
    Container container = new Container();
    container.MethodName = name;
    container.Overloads = methodGroups[name].Count;
    result.Add(container);
}
foreach (Container item in result)
{
    Console.WriteLine(item);
}

C# 3.0 Implementation

 MethodInfo[] methods = typeof(string).GetMethods();

var query =
    from m in methods
    where !m.IsStatic
    orderby m.Name
    group m by m.Name into g
    select new { MethodName = g.Key, Overloads = g.Count() };

foreach (var item in query)
{
    Console.WriteLine(item);
}

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?

kick it on DotNetKicks.com