.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.

  1. #1 by chris on May 19th, 2011

    well, if you didnt know to explain to the “fresh out of school ” employees, you are still one of them

  2. #2 by to chris on July 19th, 2012

    –>chris
    What does it have to do with the difference between the two data structurers?
    Are you one of the “fresh out of school” employees?

  3. #3 by to chris on July 19th, 2012

    chris :
    well, if you didnt know to explain to the “fresh out of school ” employees, you are still one of them

    What does it have to do with the difference between the two data structures?
    Are you one of the “fresh out of school” employees?

(will not be published)