Welcome to MSDN Blogs Sign in | Join | Help

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

Published Saturday, March 31, 2007 11:07 PM by LukeH

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Why In Memory Query with C# 3.0 is Easier than in C# 2.0

Monday, April 09, 2007 2:24 AM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# Community Convergence XXIV

Monday, April 09, 2007 2:41 AM by Charlie Calvert's Community Blog

Recently my time has been taken up with a series of internal issues involving Beta1, C# Samples and various

# re: In-Memory Query with C#2.0 and C#3.0

Wednesday, April 18, 2007 4:33 AM by ElDog

About the speed - the queue is based on generic operators, all "smart things" are performed on compile time so, carefully speaking, the queue shouldn't be much slower than the first, straightforward implementation.

# The Weekly Source Code 4

Wednesday, September 05, 2007 7:05 PM by Scott Hanselman's Computer Zen

# F#

Wednesday, November 14, 2007 6:57 PM by LukeH's WebLog

A few weeks back, Soma blogged about an increased investment by the Microsoft Developer Division in the

# F#, brought to you by Luke!

Thursday, November 15, 2007 9:34 AM by Don Syme's WebLog on F# and Other Research Projects

Luke Hoban is now full time as program manager on F#, and has just posted a short introduction about

# F#, brought to you by Luke

Thursday, November 15, 2007 9:35 AM by Don Syme's WebLog on F# and Other Research Projects

Luke Hoban is now full time as program manager on F#, and has just posted a short introduction about

# F#, brought to you by Luke

Thursday, November 15, 2007 10:06 AM by Noticias externas

Luke Hoban is now full time as program manager on F#, and has just posted a short introduction about

# Example of querying collections in C# 2.0 and 3.0 with LINQ

Friday, February 01, 2008 5:55 AM by dave^2=-1

LukeH has a good example of how simple and expressive LINQ queries can be compared to more traditional

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker