Dictionary or List?

J

Jordan

I want to store small collections of objects that have a unique id
string:"Name", so I opted to use a Dictionary<string,MyObject>
collection. However, since my collection will rarely exceed 10 or 12
items, would I be better of just using a regular List<MyObject> and
looping through the Names to find an object?

I know it's probably splitting hairs from a performance standpoint,
but I'm curious from a theoretical standpoint.

Jordan
 
G

Guest

Personally, that is "splitting hairs" to me. Use the right tool for the job,
which is this case is clearly the Dictionary.
Peter
 
N

Nicholas Paldino [.NET/C# MVP]

Jordan,

I'm glad that you see that this is probably a premature optimization
issue. However, that doesn't mean that you can't discuss it in a
theoretical sense.

Personally, choosing a data structure when I am initially coding a
solution isn't about the number of items that will be in the data structure,
but rather, what I need the data structure to do. A list and a dictionary
are two different things.

A dictionary is used when you need to perform lookups using some sort of
key. A list is used when you have a set (I use this term loosely, not in
the strict mathematical sense) of items. These items could possibly have a
key to be looked up on, but the list isn't concerned about that, it's really
just a simple container.

That being said, because you have a specific need for looking up an
object based on a key, I would day that the proper choice is the dictionary.

Of course, after you code and you start doing performance evaluations,
if you find that this is a major bottleneck, then yes, I would try other
solutions, but at this level, I would take clear, concise, understandable
and easily maintained code over code that is micro-optimized.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top