List<string> vs. string[]

  • Thread starter Thread starter Luigi
  • Start date Start date
L

Luigi

Hi all,
what are the differences (performance, features, capabilities, etc.) between
a List of string and an array of string?
I'd like to know when it's better one or another (I'm using C# 2.0)

Thanks in advance.
 
Unless you know your data is fixed size (which is quite rare), an array
is usually very prohibitive, since there is no Add/Remove. List<T>
itself also offers many additional methods compared to T[] - however,
you might not need them all for simple data like strings.

Working within an array will be marginally quicker (not much) since the
bounds checking is simpler and there is less abstraction (a List<T>
actually wraps a T[], so it can *only* get slower). In most cases, the
difference is negligible - and consider that adding data to an array
(i.e. increasing the size by 1 each time) is very slow compared to
List<T>'s doubling approach.

Marc
 
Marc Gravell said:
Unless you know your data is fixed size (which is quite rare), an array
is usually very prohibitive, since there is no Add/Remove. List<T>
itself also offers many additional methods compared to T[] - however,
you might not need them all for simple data like strings.

Working within an array will be marginally quicker (not much) since the
bounds checking is simpler and there is less abstraction (a List<T>
actually wraps a T[], so it can *only* get slower). In most cases, the
difference is negligible - and consider that adding data to an array
(i.e. increasing the size by 1 each time) is very slow compared to
List<T>'s doubling approach.

Marc

Thanks Marc, now it's clearer.

L
 
Back
Top