The difference between int [] and list <int>

What's the difference between:

int[] myIntArray 

and

  list<int> myIntArray 

?

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

+4
source share
5 answers

On the wire, they will be indistinguishable - i.e. both Customer[] and List<Customer> will look (approximately) like this:

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

Thus, it makes no sense to have logic that treats the two differently. Ultimately, int[] is a pain to work with ( Add , etc.), so I would use List<T> - of course, wsdl.exe good by default for arrays, IIRC - but there is a command line switch (or maybe for wse / wcf).

+7
source

Depends on what you will do with them.

If your dataset has a fixed size and you don't need to do any sorting, the array is better suited.

If your data needs to be more dynamic, enable sorting and you can use LINQ, then use the list

Personally, I prefer the flexibility of list types.

+3
source

They are almost the same, except that the lists have many built-in functions that make life easier for the programmer. When you start using lists, you will love them: D

In addition, arrays are fixed. You cannot (easily) resize an array.

+2
source

They are functionally equivalent, so you can use one of them. After you use one version, be consistent. This will help keep the code readable and simplify its maintenance.

+1
source

int [] is an array of integers. A list is a general list containing objects of type int

Which one you choose depends on your requirements. If you are dealing with immutable sets of objects, then int [] would be more appropriate. However, if you need to modify the contents of a collection, List provides such flexibility.

0
source

All Articles