List<string> vs. string[]

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

Marc Gravell

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
 
L

Luigi

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top