While reading a recent article in MSDN magazine, I came across some code that gives me a great opportunity to show how to get more done with less effort by using LINQ.

Here’s the code I found, showing how to build three parallel arrays from a List<BugInfo>:

DateTime[] dates = new DateTime[bugInfoList.Count];
int[] numberOpen = new int[bugInfoList.Count];
int[] numberClosed = new int[bugInfoList.Count];
for (int i = 0; i < bugInfoList.Count; ++i)
{
    dates[i] = bugInfoList[i].date;
    numberOpen[i] = bugInfoList[i].numberOpen;
    numberClosed[i] = bugInfoList[i].numberClosed;
}

The first, and obvious, suggestion to make is to use foreach instead of for:

DateTime[] dates = new DateTime[bugInfoList.Count];
int[] numberOpen = new int[bugInfoList.Count];
int[] numberClosed = new int[bugInfoList.Count];
int index = 0;
foreach(var bug in bugInfoList)
{
    dates[i] = bug.date;
    numberOpen[i] = bug.numberOpen;
    numberClosed[i] = bug.numberClosed;
    index++;
}

While this does incur the overhead of maintaining the index independently, this does avoid indexing into the list multiple times, and I’d suggest this code is easier to read.

The second, and less obvious, suggestion is to use LINQ to generate the arrays directly:

DateTime[] dates = bugInfoList.Select(b => b.date).ToArray();
int[] numberOpen = bugInfoList.Select(b => b.numberOpen).ToArray();
int[] numberClosed = bugInfoList.Select(b => b.numberClosed).ToArray();

Shorter still - very short, even - and easy to read. More importantly, many fewer opportunities to get it wrong, introducing a bug.

(Before someone flames me with the obvious: Yes, this is (technically) worse performing because it will iterate the list three times - but I’d suggest the difference isn’t likely to be significant. Micro-optimizations are seldom worth the effort. As always, don’t guess at performance, measure it with a profiler.)

Comments

blog comments powered by Disqus
Next Post
GraphVis Configuration for NAntGraph  30 Jun 2010
Prior Post
Avoiding Stack Overflow with Params  24 Jun 2010
Related Posts
Browsers and WSL  31 Mar 2024
Factory methods and functions  05 Mar 2023
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
Archives
June 2010
2010