Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best string container: StringCollection, Collection<string>, List<string>, ArrayList, ..?

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?

like image 337
abatishchev Avatar asked Mar 21 '09 10:03

abatishchev


People also ask

How do I store a list of strings?

Method#1: Using split() method The split method is used to split the strings and store them in the list. The built-in method returns a list of the words in the string, using the “delimiter” as the delimiter string.

What is StringCollection?

StringCollection is a collection of strings. In this article, learn how to take advantages of StringCollection methods and properties to manage a collection of strings.

What is string collection in C#?

StringCollection class is a new addition to the . NET Framework class library that represents a collection of strings. StringCollection class is defined in the System. Collections. Specialized namespace.


2 Answers

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)

like image 158
Marc Gravell Avatar answered Sep 20 '22 11:09

Marc Gravell


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.

like image 25
Guffa Avatar answered Sep 22 '22 11:09

Guffa