Click here to Skip to main content
13,347,168 members (48,539 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 5 May 2008

List Comprehensions for C# 2.0

, 21 May 2008
Rate this:
Please Sign up or sign in to vote.
Writing nice list comprehensions for C# 2.0.


List comprehensions are useful things to express list operations, like filtering, in a concise way. Coming from functional languages, they enter the mainstream by being popularized through languages like Python. Basically, a list comprehension says "take every object from a source collection, calculate a result from it (if it matches a certain criteria), and give me back an enumeration with all the results". This feature is also wildly popularized with C# 3.0's Linq, which extends list comprehensions by offering even more features. If you are already using C# 3.0, have a look at Linq (with LinqBridge, you can even use it with .NET Framework 2.0).

If you're stuck with VS 2005 (no C# 3.0 for you), you can at least use my implementation of list comprehensions that use a fluent interface. Its main feature is its simplicity (basically, all in one class), and it being public domain, you can just grab it, add it to your project, and pretend you wrote it on your own, without asking your software project manager for permission of adding another library.

But don't stop here, also have a look at Qwertie's Poor Man's Linq if you can afford to, this is a C# 2.0 library that implements a lot of Linq to objects features.


In typical programs, we always see an assortment of foreach loops for processing the contents of an enumerable, for example, for extracting a list of IDs from a list of domain objects:

public IList<Guid> GetIds(IEnumerable<Customer> customers)
    List l = new List<Guid>();
    foreach (Customer cust in customers)
    return l;

And, this just is performing an operation for one object without selecting.

Using the code

Using the Comprehension class, we can replace a call to GetIds() from above, to:

IList<Guid> ids = Comprehension.From(customers)
      .Select(delegate(Customer c){return cust.Id;})

In case we don't want to get all the customer IDs in the output, but only good customers' IDs, we just mix in a Where:

IEnumerable<Guid> ids = Comprehension.From(customers)
      .Select(delegate(Customer c){return cust.Id;})
      .Where(delegate(Customer c){return c.Complaints < 5;}

Oh, and did you see we have a choice of output as a full IList<> or as an IEnumerable<>?

Points of interest

You can also use this as a method to work around generic containers not being covariant. If you have a list of Customer objects but want to feed it into a method which expects a list of DomainObjects (the latter being the base class for the former), you can just use the Comprehension as an easy way to copy the elements over to a new container:

IEnumerable<DomainObject> ids = Comprehension.From(customers)


This is an updated version of this article that (hopefully) explains list comprehensions better and provides some of the information I received in the comments. Thank you for your input, people :-) It will be my last article on C# 2.0 because Roger Aisling made me switch to 2008, programming in C# 3.0, targeting .NET 2.0 for the production code and .NET 3.5 (with Rhino Mocks 3.5 beta) for the test cases.


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Frohwalt Egerer
Software Developer Egerer + Tandogan GmbH
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralInspiring Pin
Herre Kuijpers5-May-08 10:27
memberHerre Kuijpers5-May-08 10:27 
GeneralRe: Inspiring Pin
mariahayek5-May-08 12:54
membermariahayek5-May-08 12:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 21 May 2008
Article Copyright 2008 by Frohwalt Egerer
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid