다음을 통해 공유


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

Comments

  • Anonymous
    April 08, 2007
    You've been kicked (a good thing) - Trackback from DotNetKicks.com

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

  • Anonymous
    April 17, 2007
    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.

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

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

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

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

  • Anonymous
    January 31, 2008
    LukeH has a good example of how simple and expressive LINQ queries can be compared to more traditional

  • Anonymous
    June 16, 2008
    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!

  • Anonymous
    August 03, 2008
    The comment has been removed

  • Anonymous
    September 11, 2008
    The comment has been removed