.Net Collection<T> vs. List<T>
Recently, we have gotten a couple of “fresh out of school” employees, and here the other day we went through the code of one of our applications and explained how it was built (at least how was supposed to be or “do as I tell you, don’t do as I do”).
Our O/R-mapper returns a generic Collection<T> when it is asked for a list / collection of some objects, and is consistent in doing so. The question however, was why that wasn’t a List<T>.
I had to admit I hadn’t dug into the material properly, so the question kindof got left there in open air. My bad excuse was of course I had been mostly doing managment stuff lately, and so was my companion presenter as well.
So to make a not so long story short, I had to dig into the collections that is found in .NET, and I explain them to myself for later reference, beginning with the aforementioned Collection<T> and List<T>.
The difference
Just to sum it up, Collection<T> is made for extensibility and List<T> is made for performance. See this and this from the FxCop guys.
For a full blown explanation, a great reference is also found here.
Use List<T> for all your heavy lifting internally, and expose a Collection<T> in your public API.
Haugerns Development Escapades » Blog Archive » Beware of the IEnumerables:
[…] time ago I wrote about the difference between List and Collection, and how you should use them. Usually you want to keep the details away from the users of your […]
17 October 2008, 11:00 am