Dictionary vs ArrayList

When adding data to a collection, what is best to use and what is performance. Impact if we use dictionary vs ArrayList and why?

+2


a source to share


2 answers


You shouldn't actually use ArrayList

, as you are using strongly typed List<T>

.

What you use depends on how you need to access the data. A list stores a sequential list of items, while a dictionary stores items identified by a key. (You can still read items from the Dictionary sequentially, but the order is not preserved.)



The performance is almost the same as the internal arrays for storing actual data. When they reach their capacity, they allocate a new larger array and copy the data to it. If you know how big the collection is, you must specify the capacity when you create it so that it doesn't change on its own.

+3


a source


They are not interchangeable classes. Apples and oranges. If you intend to search for items in the collection by key, use Dictionary

. Otherwise use ArrayList

(or preferably List<T>

)



+1


a source







All Articles