MyClass[] array;
List<MyClass> list;
What are the scenarios when one is preferable over the other? And why?
MyClass[] array;
List<MyClass> list;
What are the scenarios when one is preferable over the other? And why?
It is rare, in reality, that you would want to use an array. Definitely use a List<T>
any time you want to add/remove data, since resizing arrays is expensive. If you know the data is fixed length, and you want to micro-optimise for some very specific reason (after benchmarking), then an array may be useful.
List<T>
offers a lot more functionality than an array (although LINQ evens it up a bit), and is almost always the right choice. Except for params
arguments, of course ;-p
As a counter - List<T>
is one-dimensional; where-as you have have rectangular (etc) arrays like int[,]
or string[,,]
- but there are other ways of modelling such data (if you need) in an object model.
See also:
That said, I make a lot of use of arrays in my protobuf-net project; entirely for performance:
byte[]
buffer which I fill before sending down to the underlying stream (and v.v.); quicker than BufferedStream
etcFoo[]
rather than List<Foo>
), since the size is fixed once built, and needs to be very fastBut this is definitely an exception; for general line-of-business processing, a List<T>
wins every time.
Unless you are really concerned with performance, and by that I mean, "Why are you using .Net instead of C++?" you should stick with List<>. It's easier to maintain and does all the dirty work of resizing an array behind the scenes for you. (If necessary, List<> is pretty smart about choosing array sizes so it doesn't need to usually.)
Arrays are rather obsolete, as seen in a popular discussion here. Also pointed out here, and by our host in the blog.
It completely depends on the contexts in which the data structure is needed. For example, if you are creating items to be used by other functions or services using List is the perfect way to accomplish it.
Now if you have a list of items and you just want to display them, say on a web page array is the container you need to use.
Agree with Marc + for interoperability you can't have List<> as argument of a method
Notwithstanding the other answers recommending List<T>
, you'll want to use arrays when handling:
Really just answering to add a link which I'm surprised hasn't been mentioned yet: Eric's Lippert's blog entry on "Arrays considered somewhat harmful."
You can judge from the title that it's suggesting using collections wherever practical - but as Marc rightly points out, there are plenty of places where an array really is the only practical solution.
If I know exactly how many elements I'm going to need, say I need 5 elements and only ever 5 elements then I use an array. Otherwise I just use a List<T>.
Use an array when you are dealing with data that is:
Use a list for:
Use a hashmap for:
In reality, you'll want a list or hashmap almost all of the time. Next time you pick a data structure, think about what it must do well for you (or your code, anyway). Then pick something based on that. When in doubt, pick something as general as possible, i.e. an interface you can replace the implementation of quite easily. Some good links in the other answers as well.
If you want to lookup/edit data in random places, use array
for speed.
If you just need to iterate the collection, or add/remove data, definitely use a (linked) list
.
Also List
s for improved memory usage, an array
has to be consecutively arranged in memory, so you can lookup the individual array
members by getting the address of the array
and adding the element's index. This gives a O(1) lookup time for array
s :) A list
's lookup time would be O(n).