gpt4 book ai didi

c# - 键值对的有序列表?

转载 作者:太空狗 更新时间:2023-10-29 19:50:31 24 4
gpt4 key购买 nike

.net 中是否有允许存储的集合 KeyValuePair<string, string>保持插入的顺序?
OrderedDictionary 看起来很有前途,但似乎相当缺乏。
现在我正在研究 IOrderedEnumerable>,但我似乎找不到除 ISortedDictionary 之外的任何实现,但这不是我想要的。不需要进行排序,插入的顺序很重要。

更新
我不喜欢 OrderedDictionary 的原因是它不通用。

最佳答案

虽然我迟到了,但 .NET Framework 4.5 为您提供了新类。参见 SortedList<TKey, TValue> SortedDictionary<TKey, TValue> .如果您想知道应该使用哪一个,MSDN 提供了一些您可以选择其中一个的充分理由。

The SortedList generic class is an array of key/value pairs with O(log n) retrieval, where n is the number of elements in the dictionary. In this, it is similar to the SortedDictionary generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal:

  • SortedList<TKey, TValue> uses less memory than SortedDictionary<TKey, TValue>.
  • SortedDictionary<TKey, TValue> has faster insertion and removal operations for unsorted data, O(log n) as opposed to O(n) for SortedList<TKey, TValue>.
  • If the list is populated all at once from sorted data, SortedList<TKey, TValue> is faster than SortedDictionary<TKey, TValue>.

Another difference between the SortedDictionary<TKey, TValue> and SortedList<TKey, TValue> classes is that SortedList<TKey, TValue> supports efficient indexed retrieval of keys and values through the collections returned by the Keys and Values properties. It is not necessary to regenerate the lists when the properties are accessed, because the lists are just wrappers for the internal arrays of keys and values.

两个链接都有相似的备注部分(这是引用的来源)。他们还提供有关这两个类(class)的更多信息。如果您有兴趣使用其中的一部分,我建议您阅读这两个部分。

关于c# - 键值对的有序列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3070644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com