What is the difference between T [] and List & lt; T & gt;


Actually I'm always using Generic Collections and I frequently use List<>. Some scenarios I think that new List<string>() is very ugly and I prefer to use string[], but I don't use it, because as far as I know, Generics has a better performance and therefore I use them.

Is string[], int[] or whatever non-generic-array harmful to the application?

I just wanna know what's really the difference, impact between arrays and generic collections.


let's fake a scenario

I must call this method, should I use string[] or List<string>? What's better?

static void PrintValues(IEnumerable<string> values) {
    foreach(var value in values) {

The main difference is that you can add new elements to a List<T>.

Internally List<T> stores elements in an array of type T[] and it just automatically allocates a larger array when you're adding new elements (or shrinks the array when you're removing elements). This means that the performance will be roughly similar. There is some minor indirection when using List<T>, but the JITter may inline that.

The main reason for using List<T> is that it gives you more functionality - you can add and remove elements.

EDIT In your example with PrintValues, it doesn't really matter. It depends on the source of the data. If you just want to call it with some fixed arguments you can use arrays, because they're easier to create, e.g. new [] { "hello", "world" }.

In most of the real-world scenarios, you'll be reading the data from somewhere - if you can get all data at once, then you can use arrays. If you'll reading them one-by-one then you'll use List<string> so that you can add elements as you read them.