Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use a Dictionary for collections with 10 items or less, or is there a better alternative?

I have a list of objects and I need to find an object as quickly as possible (by it's name property). What data-structure should I use? I know I can use a Dictionary, but there wont ever be more than 10 items in the list, and if I remember correctly the dictionary is implemented as an array if the collection contains 10 items or less.

Thanks.

like image 689
Mikael Sundberg Avatar asked Mar 10 '10 18:03

Mikael Sundberg


2 Answers

MSDN recommends the ListDictionary for collections with 10 items or less:

This is a simple implementation of IDictionary using a singly linked list. It is smaller and faster than a Hashtable if the number of elements is 10 or less. This should not be used if performance is important for large numbers of elements.

like image 140
Adam Lear Avatar answered Sep 28 '22 16:09

Adam Lear


You may want to consider the System.Collections.Specialized.ListDictionary if you are certain there will be less than ten items.

Also consider the System.Collections.Specialized.HybridDictionary which switches behaviour (with a small overhead) should the size increase above a threshold, handy if your assumption is wrong.

like image 30
Paul Ruane Avatar answered Sep 28 '22 16:09

Paul Ruane