39

I was wondering when I should use List< string > and when I should use StringCollection.

Let's say that I have to deal with large number of strings (like text files of 10mb).

I know that List< T > provides more powerful functions than StringCollection.

But sometimes I kind of find the List< T > slow when for example telling a Gridview that its datasource is a List< String >...

So do anyone know the pros and cons of these collections, concerning speed and weight in memory?

Concerning their functionalities, I am sure everybody will agree to say that List is the best, so my question is not about that. Consider the question is about projects on Frameworks 4.0, so both can be used.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
GianT971
  • 4,385
  • 7
  • 34
  • 46

3 Answers3

52

I would personally prefer to use List<string>:

  • No need to remember one specific type just for strings
  • It implements the generic IEnumerable<T> rather than just IEnumerable, and thus supports LINQ
  • It's supported in SilverLight
  • It's more idiomatic for most developers (IMO)

I would be really surprised to find StringCollection to be significantly faster than List<string> - see if you can back that up with numbers. My only cause for hesitation is that GridView could potentially have hard-coded support for StringCollection to make it fast with that type - but that sounds pretty unlikely to me.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 7
    Woe, that's right! StringCollection wouldn't sport IEnumerable. Huge factor. Yeah, throw StringCollection away. – Patrick Karcher Oct 14 '11 at 23:12
  • 1
    About speed concern, I noticed that it changes a lot, sometimes StringCollection is faster, sometimes it's the opposite. But what about the memory? Does List take more place in memory than StringCollection? – GianT971 Oct 15 '11 at 00:19
  • 8
    @GianT971 If you look at the `StringCollection` code with IlSpy you'll see that `StringCollection` is a shim around `ArrayList` that gives some strongly typed methods to add, remove... Now, considering that `ArrayList` and `List<>` use the same growth algorithm (*2 of old size each time) there shouldn't be any meaningful difference in speed or memory size. – xanatos Oct 15 '11 at 06:52
26

In terms of performance and efficiency, they will be very similar.

List<string> might be a little faster actually. It is kindof a wrapper around the pre-generic ArrayList. There's no boxing/unboxing, but there is still an extra step or two under the hood, IIRC.

StringCollection was handy before .NET 2.0 because it was strongly typed to string, very common thing to want a list of. I would suggest using List<string> now though. Since most framework and 3rd party assemblies will use it rather than StringCollection, this would:

  • avoid a lot of casting
  • avoid some confusion. Other (especially newer) developers would constantly be wondering what your reason was for using StringCollection.
Patrick Karcher
  • 22,995
  • 5
  • 52
  • 66
4

List<string> is not a wrapper over ArrayList

It is a new implementation of ArrayList implemented by having an array (which is resized to the double size when the count becomes bigger than its length) and a count property.

ysf
  • 4,634
  • 3
  • 27
  • 29
Paulo Zemek
  • 447
  • 5
  • 5