32

What is the most suitable container just for strings holding in some array with non-predetermined upper boundary, which length is unknown on it's creation.

For simple code like:

var list = new SomeContainer(); // size is unknown
for()/foreach()/do()/while() // any loop
{
  list.Add(string);
}

Is it StringCollection as optimized Collection for string, or just Collection<string> or List<string> or ArrayList ? What is the different between them?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • Related question concerning performance specifically: https://stackoverflow.com/questions/7774374/performance-concern-stringcollection-vs-liststring – Knowledge Cube Jun 26 '17 at 16:41

2 Answers2

37

For what you need, List<string> is probably the most versatile. StringCollection was handy in 1.1, when (without generics) you needed specific classes for typed collections. Collection<string> is handy if you want to subclass it to provide validation etc.

Either way, in 3.5, LINQ will provide lots of additional methods for them - but without LINQ, List<T> has more features (Find, Sort, etc)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
15

A List<T> would be the most efficient.

The Collection<T> class is just a wrapper for another type of list, the parameterless constructor initialises the Collection<T> with a List<T>.

An ArrayList is essentially the same as a List<object>, so you would have to cast the items to String when reading from it.

A StringCollection is just a type safe wrapper for an ArrayList.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005