Back when .NET 2 was released in 2006 Lists appeared and I remember having one of those “OMG this is fantastic moment for coding“. You could immediately see the benefits – how they’d reduce the amount of code you had to write and increase readability. Prior to that in .NET 1.1 there was IEnumerable, which I can’ t say was something I made a lot direct use of, with emphasis on the word “direct”. I utilised a number of classes that implemented IEnumerable (SortedList, Hashtable, List, etc.) but, unlike today, it wasn’t a class that I worked with directly. Then a few years later .NET 3.5 was released and IQueryable was brought into the mix and suddenly I started having to think a touch more about which one was the “right” one to use when it came to manipulating a collection of objects.
I work with object collections a lot, whether it be generating them in code, loading and manipulating them from a database, generating them from input etc. and it’s always important for me to know the best way of manipulating them – hopefully this applies to you as well, otherwise feel free to call me narcissistic :). Microsoft makes all these great coding tools available to the developer, and as a developer you want to use the right tool for the job … right? :). So I thought I’d write a blog about this dated, but still commonly asked question.
What’s the Difference?
Here’s the simple way to think of the three classes/interfaces – and I’ve purposefully removed a lot of technical jargon, i.e. no talk of Expression objects or Func object:
- List – A strongly typed list of in-memory objects that can be manipulated in just about every way possible. You can do a lot with a List of objects; sorting, counting, does or doesn’t contain, finding, removing etc. – it’s like the Swiss Army knife of collections
- IEnumerable – Ignoring the fact that it’s an interface for a moment, it’s simpler version of a List – it works with objects in-memory but it’s forward moving only and doesn’t expose a lot of members/methods that List does.
- IQueryable – First off it’s important to note that this interface extends IEnumerable, and there are 3 key differences; (i) it points to a query provider (database, web services, etc.), (ii) it uses a query expression to correctly translate what it’s asking for from the data source in question, and (iii) it doesn’t work with objects in-memory. IQueryable is more commonly found when working with LINQ to SQL or Entity Framework.
If I’m writing a new class that will require some form of collection request and manipulation I always ask myself these two questions to help me pinpoint the appropriate class/interface:
- Am I interfacing with a data source, or just working with object in-memory?
- What do I want to do with the collection once I have it?
The answers usually provide me with the right class/interface.
A Bit More…
So List and IEnumerable are similar in that they:
- They are not a query, but a collection of objects
- Allow you to work with objects in-memory
- IQueryable extends IEnumerable
- In the case of LINQ you can keep extending it and execute the LINQ when you’re ready
Also keep an eye on performance and compare the two queries using the appropriate tools if you want to delve deeper into what’s going on under the hood.