tags:

views:

336

answers:

8

In C# There seem to be quite a few different lists. Off the top of my head I was able to come up with a couple, however I'm sure there are many more.

List<String> Types = new List<String>();
ArrayList Types2 = new ArrayList();
LinkedList<String> Types4 = new LinkedList<String>();

My question is when is it beneficial to use one over the other?

More specifically I am returning lists of unknown size from functions and I was wondering if there is a particular list that was better at this.

+1  A: 

Generally, use List. Don't use ArrayList; it's obsolete. Use LinkedList in the rare cases where you need to be able to add without resizing and don't mind the overhead and loss of random access.

Steven Sudit
Any particular reason for downvoting this correct answer?
Steven Sudit
I'm going to conclude that this is an example of strategic voting. How interesting.
Steven Sudit
+2  A: 
List<String> Types = new List<String>();
LinkedList<String> Types4 = new LinkedList<String>();

are generic lists, i.e. you define the data type that would go in there which decreased boxing and un-boxing.

for difference in list vs linklist, see this --> http://stackoverflow.com/questions/169973/when-should-i-use-a-list-vs-a-linkedlist

ArrayList is a non-generic collection, which can be used to store any type of data type.

Bhaskar
List can also be used to store any type of data, so this is a misleading answer. List can be specialized on a type, or on Object.
Steven Sudit
A: 

ArrayList is probably smaller, memory-wise, since it is based on an array. It also has fast random-access to elements. However, adding or removing to the list will take longer. This might be sped up slightly if the object over-allocates under the assumption that you are going to keep adding. (That will, of course, reduce the memory advantage.)

The other lists will be slightly larger (4-to-8 bytes more memory per element), and will have poor random access times. However, it is very fast to add or remove objects to the ends of the list. Also, memory usage is usually spot-on for what you need.

Christopher
Actually, an ArrayList is usually larger since it's basically an array of Objects (at least for any non-trivial sized list).
Jess
@LuckyLindy:Template lists are better, sure. But the memory for the array will not be significantly larger unless you are storing simple integers or floating point numbers. Everything else descends from object anyway.
Christopher
LuckyLindy is correct: ArrayList will not be smaller but will often be larger.
Steven Sudit
@LuckyLindy, @Steven, can you point to any documents that support your assertions?
Christopher
@Christopher: For value types, ArrayList has to box them, and the box is always bigger than the contents. List<T> generates a version for each non-reference T. For reference types, it stores a pointer regardless, but List<T> avoids the cost of downcasting. In short, there is no advantage to ArrayList and it should be cvonsidered obsolete.
Steven Sudit
@Steven, an object which descends from Object doesn't have to be boxed, so there is now size increase. Any downcasting is a speed consideration, not a size consideration. As for the rest of what you said, that is exactly what I said in my first reply to LuckyLindy. I am not promoting the use of ArrayList, merely indicating that there is no size advantage to the template version when storing actual objects.
Christopher
All objects descend from Object; I think what you mean is that an object that does not descend from ValueType does not need to be boxed. That's true, so the size advantage is only for value types.
Steven Sudit
@Christopher, your original answer said ArrayList is smaller and has better random access times. I believe it's been explained how this is incorrect.
Steven Sudit
+1  A: 

See the article on Commonly Used Collection Types from MSDN for a list of the the various types of collections available to you, and their intended uses.

bdukes
+2  A: 

99% of the time List is what you'll want. Avoid the non-generic collections at all costs.

LinkedList is useful for adding or removing without shuffling items around, although you have to forego random access as a result. One advantage it does have is you can remove items whilst iterating through the nodes.

Sean
+2  A: 

ArrayList is a holdover from before Generics. There's really no reason to use them ... they're slow and use more memory than List<>. In general, there's probably no reason to use LinkedList either unless you are inserting midway through VERY large lists.

The only thing you'll find in .NET faster than a List<> is a fixed array ... but the performance difference is surprisingly small.

Jess
+1  A: 

ArrayList is a .Net 1.0 list type. List is a generic list introduced with generics in .Net 2.0.

Generic lists provide better compile time support. Generics lists are type safe. You cannot add objects of wrong type. Therefor you know which type the stored objects has. There are no typechecks and typecasts nessecary.

I dont know about performance differences.

This questions says something about the difference of List and LinkedList.

Christian13467
+1  A: 

As mentioned, don't use ArrayList if at all possible.

Here's an bit on Wikipedia about the differences between arrays and linked lists.

In summary:

Arrays

  • Fast random access
  • Fast inserting/deleting at end
  • Good memory locality

Linked Lists

  • Fast inserting/deleting at beginning
  • Fast inserting/deleting at end
  • Fast inserting/deleting at middle (with enumerator)
John Calsbeek