Is the list order of a ConcurrentDictionary guaranteed?

人盡茶涼 提交于 2019-12-20 03:41:15

问题


I am using a ConcurrentDictionary to store log-lines, and when I need to display them to the user I call ToList() to generate a list. But the weird thing is that some users receive the most recent lines first in the list, while they should logically be last.

Is this because ConcurrentDictionary doesnt guarantee a persistent order on the IEnumerate interface, or what can be the reason?


回答1:


No ConcurrentDictionary (and Dictionary<T> for that matter) does not guarantee the ordering of the keys in the list. You'll have to use a different data type or perform the sorting yourself. For non-concurrent code you would use SortedDictionary<T>, but I don't believe there is an analogue in the concurrent collections.




回答2:


No. The list order of ConcurrentDictionary is NOT guaranteed, lines can come out in any order.



来源:https://stackoverflow.com/questions/20020074/is-the-list-order-of-a-concurrentdictionary-guaranteed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!