当前位置: 动力学知识库 > 问答 > 编程问答 >

.net - any reason to not use a dictionary

问题描述:

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

网友答案:

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.

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: