1

I was having a discussion with a co-worker over whether it is better to use a Dictionary and repopulate it whenever a result set changes or to just use linq loop over all elements in the list each time.

We're trying to map a parent / child relationship, and I suggested using the ParentID as the dictionary key, and the dictionary value as the custom object.

We will usually be needing to iterate over all parent / children and run down the list.

Is there any reason why you wouldn't want to use a Dictionary in this circumstance? if so what would you use instead

Seph
  • 8,472
  • 10
  • 63
  • 94
  • 1
    Code both and profile to see which wins. It is really hard to determine what is a good recommendation without a lot more information on the exact scenario. For example: how big is your common load, how big a load do you need to support (future-proofing), which code is simpler (your description leave a lot to be desired - real code would help), is this even a bottleneck for your app, or are you just shooting the breeze, etc. – Merlyn Morgan-Graham Oct 28 '10 at 05:57

2 Answers2

1

Performance wise, it's really going to depend on your specific application, so you'll have to test.

Toby
  • 1,630
  • 11
  • 7
1

If you really do need to iterate over the whole collection every time, there would not be much (if any) benefit to having a Dictionary. On the other hand, if you occasionally need to locate specific instances by their keys, Dictionary could be what you need.

Note that even if you are using a Dictionary, you can still just iterate through the list of Values when needed, for not too much more 'weight' than iterating through a normal List, but then you'd still have the Dictionary capabilities, if needed.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123