views:

165

answers:

5

What is the difference between:

    int[] myIntArray

and

 list<int> myIntArray

?

In web services I read that I should pick one or the other and not mix the two, why?

+6  A: 

On the wire they will be indistinguishable - i.e. both Customer[] and List<Customer> are going to look (roughly) like:

<Customers>
   <Customer name="Fred" ... />
   <Customer name="Barney" ... />
</Customers>

So there is no point having logic that treats the two differently. Ultimately, int[] is a pain to work with (Add etc), so I would tend to use List<T> - of course, wsdl.exe nicely defaults to arrays, IIRC - but there is a command line switch (or perhaps there is for wse/wcf).

Marc Gravell
+3  A: 

Depends on what you will be doing with them.

If your dataset is of a fixed size and you will not need to do any sorting, the array is better suited.

If your data needs to be more dynamic, allow for sorting, and be able to take advantage of LINQ, then use the list

Personally, I prefer the flexibility of list types.

Chris Ballance
There is the `Array.Sort` static method if you want to sort an array. LINQ also has little to do with it, as arrays are also `IEnumerable`. Being resizable is basically all that matters.
Joren
@Joren Yes Array.Sort exists, but how does that sorting happen under the hood? It can't possibly be as efficent as sorting a list due to the way it is stored in memory.
Chris Ballance
Huh? `List` uses an array as backing storage you know. The sort methods are probably almost identical.
Joren
I took a quick peek with Reflector, and `List.Sort` actually **calls** `Array.Sort` to do its work.
Joren
A: 

int[] is an array of integers. List is a generic list that holds objects of type int

Which one you pick depends on your requirements. If you are dealing with immutable collections of objects, int[] would be more suitable. However, if you need to modify the contents of the collection, List provides that flexibility.

pmarflee
Allowing you to modify the contents or not is not a difference between arrays and `List`. Both allow you to. Arrays however do not allow you to modify the *number* of items.
Joren
+1  A: 

They are functionally equivalent, so you can use either one. Once you use one version, be consistent. It'll help keep your code readable and easier to maintain.

Evan Meagher
+1  A: 

They are pretty much the same, except that Lists have a lot of built in functions to ease the life of the programmer. Once you start using lists, you'll love them :D

Also, arrays are fixed. You cannnot (easily) change the size of an array.

Sergio Tapia