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

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

  • Comments 12

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

Leave a Comment
  • Please add 7 and 3 and type the answer here:
  • Post
  • You've been kicked (a good thing) - Trackback from DotNetKicks.com

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

  • 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.

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

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

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

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

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

  • Hi Luke,

    I think there is a slight bug in the C# 2.0 implementation in that you are relying on the ordering of the Dictionary.Keys collection to be the same as what was inserted. This may work in practice, but according to the docs:

    The order of the keys in the Dictionary<(Of <(TKey, TValue>)>).KeyCollection is unspecified

    Perhaps you should use a SortedDictionary and ditch the sort on the list of MethodInfos?

    I suppose it just goes to further demonstrate the beauty of C# 3.0: the code is terser, the objective of the code is more obvious and it is easier to get right!

  • Propecia. Cheap propecia. Propecia online uk.

  • The second implementation is much slower. But let's explain why.

    LINQ was made to embed SQL into the code. Therefore it was supposed to use all the database "good things" to do the job.

    For me the only database good things are "indexes". The way to access one record in a million in less than 0.1 seconds. Therefore when LINQ is translated in a SQL query of an indexed database we obtain:

    1. good performance

    2. good readability.

    But some times we need to handle in memory data. In that case we thing "hey! let's use linq again" But think now "who is gonna support your efforts when making queries?". The answer is nobody.

    There is no special software for LINQ to objects. Then what linq does is to "sweep" the data structure collecting members that match your query.

    Of course this is slow as hell if you compare it with a dictionary.

    If you're gonna do it once. Is ok to wait because we will suffer it only once. But if the query is in a loop. You're delete the code after the second run because is just too much time.

Page 1 of 1 (12 items)