是否可以确保ConcurrentDictionary的列表顺序? [英] Is the list order of a ConcurrentDictionary guaranteed?
问题描述
我正在使用ConcurrentDictionary存储日志行,当需要向用户显示日志行时,我致电ToList()
生成列表.但是奇怪的是,某些用户在列表中排在第一位,而在逻辑上应该排在最后.
这是因为ConcurrentDictionary不保证IEnumerate接口上的持久性顺序吗?或者是什么原因?
否ConcurrentDictionary
(和Dictionary<T>
)不能保证列表中键的顺序.您将必须使用其他数据类型或自己执行排序.对于非并行代码,您可以使用 SortedDictionary<T>
,但我不认为并发集合中有类似的东西.
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?
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.
这篇关于是否可以确保ConcurrentDictionary的列表顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!