本文介绍了ConcurrentDictionary 的列表顺序是否有保证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ConcurrentDictionary 来存储日志行,当我需要向用户显示它们时,我调用 ToList() 来生成一个列表.但奇怪的是,有些用户在列表中最先收到最近的行,而逻辑上他们应该在最后.

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.

这是因为 ConcurrentDictionary 不能保证 IEnumerate 接口上的持久顺序,或者可能是什么原因?

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

推荐答案

No ConcurrentDictionary(和 Dictionary 就此而言)不保证列表中的键.您必须使用不同的数据类型或自己执行排序.对于非并发代码,您将使用 SortedDictionary,但我认为并发集合中没有类似物.

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 的列表顺序是否有保证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 11:05