While Adding the data into the collection, which is better practice to use, and what is performance Impact if we use Dictionary vs ArrayList and Why?
-
possible duplicate of http://stackoverflow.com/questions/128636/net-data-structures-arraylist-list-hashtable-dictionary-sortedlist-sortedd – Michael Todd Apr 22 '10 at 05:13
-
2It depends entirely on what you're doing *after* you add data into the collection, surely? What's the point of adding stuff to a collection if you never do anything with it once it's in there? – Dean Harding Apr 22 '10 at 05:14
2 Answers
You should actually not use ArrayList
at all, as you have the strongly typed List<T>
to use.
Which you use depends on how you need to access the data. The List stores a sequential list of items, while a Dictionary stores items identified by a key. (You can still read the items from the Dictionary sequentially, but the order is not preserved.)
The performance is pretty much the same, both uses arrays internally to store the actual data. When they reach their capacity they allocate a new larger array and copies the data to it. If you know how large the collection will get, you should specify the capacity when you create it, so that it doesn't have to resize itself.

- 687,336
- 108
- 737
- 1,005
They are not interchangeable classes. Apples and oranges. If you intend to look up items in the collection by a key, use Dictionary
. Otherwise, use ArrayList
(or preferably List<T>
)

- 68,005
- 14
- 144
- 156