67

We can use any of these (includes List, ArrayList, Dictionary, Hashtable, Stack, Queue) to hold value or hold reference to other objects as a collection.

But, my question is which one is used when?

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
Sujit
  • 3,677
  • 9
  • 41
  • 50
  • 2
    You can learn all of .Net and C# by asking a series of questions here, but much easier all round would be to buy a good book! – Justin Harvey Aug 10 '12 at 13:00
  • 14
    @Baboon: No one gets banned for asking questions like this on SO. – Tim Schmelter Aug 10 '12 at 13:04
  • 2
    @TimSchmelter This one is already quite bordeline: there is an obvious lack of preliminary research, and the question is too broad. Furthermore, I meant "you can't learn all of .Net and C# by asking a series of questions on SO" because you'll end up asking lots of duplicate, uninteresting, lack-of-research questions – Louis Kottmann Aug 10 '12 at 13:10
  • Microsoft provides a brief comparison [here](https://learn.microsoft.com/en-us/dotnet/standard/collections/#choose-a-collection). – michuu May 12 '23 at 16:08

3 Answers3

109

Lists

Lists allow duplicate items, can be accessed by index, and support linear traversal.

  • ArrayList - An array-based list that doesn't support generic types. It does not enforce type safety and should generally be avoided.

  • List - An array list that supports generic types and enforces type-safety. Since it is non-contiguous, it can grow in size without re-allocating memory for the entire list. This is the more commonly used list collection.

Hashes

Hashes are look-ups in which you give each item in a list a "key" which will be used to retrieve it later. Think of a hash like a table index where you can ask questions like "I'm going to find this object by this string value. Duplicate keys are not allowed.

  • HashTable - A basic key-value-pair map that functions like an indexed list.

  • Dictionary - A hashtable that supports generic types and enforces type-safety.

Queues

Queues control how items in a list are accessed. You typically push/pop records from a queue in a particular direction (from either the front or back). Not used for random access in the middle.

  • Stack - A LIFO (last in, first out) list where you push/pop records on top of each other.

  • Queue - A FIFO (first in, first out) list where you push records on top and pop them off the bottom.

svick
  • 236,525
  • 50
  • 385
  • 514
Jordan Parmer
  • 36,042
  • 30
  • 97
  • 119
14
  • List can hold duplicate objects

  • ArrayList is just for compatibility with older versions of the framework where IList didn't exist

  • Dictionary is used to store pairs of key/value. You cannot have duplicate keys.

  • Hashtable is basically a List with no possibility of duplicates (and better performance in some scenarios)

  • Stack stores objects in order they were added (through Push()), and when you retrieve an object (through Pop()) it is removed from the stack in a LIFO manner.

  • Queue quite similar to a Stack except it is FIFO.

Louis Kottmann
  • 16,268
  • 4
  • 64
  • 88
  • What is the difference between a) cannot have duplicate keys and b) no possibility of duplicates – Blue Clouds May 07 '15 at 19:45
  • 1
    There is no diference in this case. Both could not store duplicate keys. And besides the performance issue, the Hashtable has not generic implementation. – Andre Soares Jun 30 '15 at 16:41
  • You haven't differentiated well between Hashtable and Dictionary – Chandraprakash Oct 16 '21 at 19:48
  • @Chandraprakash To help you and other people looking for an answer: **Hashtable is a loosely typed (non-generic) collection, Dictionary is a generic collection. This means that Hashtable can use both keys and values of any type, Dictionary [asks](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=net-6.0#constructors) during construction what type of keys and values you are going to use.** – online Thomas Jan 06 '22 at 10:13
4

Here are some uses for them.

List: If you just want a list and don't care about any duplicates, i.e list of people, shopping list, list of things to do in life.

Queues: If you want to simulate a queue for example, in a hospital you have a queue and also priority queue (in emergency departments). The triage would determine who is in critical condition and needs to be treated.

Another example is a shopping queue, first person in line is 'usually' the first one to checkout.

Stacks: Used in your internal memory to push and pop values as you pass them to functions/methods.

Another interesting use is, in video game inventory method, where you can pick up an item (push) onto the stack, and drop an item (pop) off the stack.

Hash/Dictionary: These are usually seen used in database, for look up and index.

Depending on what you want to simulate, I do agree with the others, it's handy to read up on data-structures. A book helps but the internet also has a wealth of information.

Th0rndike
  • 3,406
  • 3
  • 22
  • 42
TheLazyChap
  • 1,852
  • 1
  • 19
  • 32