6

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.

corymathews
  • 12,289
  • 14
  • 57
  • 77

8 Answers8

2
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 --> 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.

Community
  • 1
  • 1
Bhaskar
  • 10,537
  • 6
  • 53
  • 64
  • 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 Aug 12 '09 at 12:53
2

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
  • 60,939
  • 11
  • 97
  • 136
2

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.

Beep beep
  • 18,873
  • 12
  • 63
  • 78
1

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
  • 19,391
  • 1
  • 51
  • 53
1

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
  • 152,002
  • 23
  • 148
  • 175
1

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.

Community
  • 1
  • 1
Christian13467
  • 5,324
  • 31
  • 34
1

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
  • 35,947
  • 7
  • 94
  • 101
-2

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
  • 8,815
  • 2
  • 32
  • 41
  • Actually, an ArrayList is usually larger since it's basically an array of Objects (at least for any non-trivial sized list). – Beep beep Aug 11 '09 at 15:12
  • @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 Aug 11 '09 at 17:52
  • LuckyLindy is correct: ArrayList will not be smaller but will often be larger. – Steven Sudit Aug 12 '09 at 12:54
  • @LuckyLindy, @Steven, can you point to any documents that support your assertions? – Christopher Aug 12 '09 at 18:58
  • @Christopher: For value types, ArrayList has to box them, and the box is always bigger than the contents. List generates a version for each non-reference T. For reference types, it stores a pointer regardless, but List avoids the cost of downcasting. In short, there is no advantage to ArrayList and it should be cvonsidered obsolete. – Steven Sudit Aug 15 '09 at 17:40
  • @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 Aug 15 '09 at 18:02
  • 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 Aug 15 '09 at 23:45
  • @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 Aug 15 '09 at 23:48