C#中的保留顺序数据结构 [英] Order-preserving data structures in C#
问题描述
MSDN没有有关数据结构的顺序保留属性的信息。因此,我一直假设:
MSDN has no information on the order preserving properties of data structures. So I've been making the assumption that:
- HashTable和HashSet 不保留插入顺序(即
- 字典和列表 do 保留插入顺序。
- HashTable and HashSet do not preserve the insertion order (i.e. the "hash" in there is a giveaway)
- Dictionary and List do preserve the insertion order.
据此我推断如果我有 Dictionary< double,double> foo
定义一条曲线, foo.Keys.ToList()
和 foo.Values.ToList()
会给我该曲线的范围和域的有序列表,而不会弄乱它吗?
From this I extrapolate that if I have a Dictionary<double, double> foo
that defines a curve, foo.Keys.ToList()
and foo.Values.ToList()
will give me an ordered list of the scope and domain of that curve without messing about with it?
推荐答案
您不应在常规 Dictionary< TKey,TValue>
以任何顺序进行维护。在 SortedDictionary< TKey,TValue>
键和值由键的值顺序维护-这与插入顺序不同。
.NET框架中唯一保留插入顺序的内置字典是 System.Collections.Specialized.OrderedDictionary
。不幸的是,该类不是通用类-但是,围绕它编写通用包装并不困难。请记住,在处理值类型(例如 int
或 double
)时,将导致键/值(通用字典不对值类型强加拳击)。
The only built-in dictionary in the .NET framework that preserves insertion order is System.Collections.Specialized.OrderedDictionary
. Unfortunately, this class is not generic - however, it's not terribly hard to write a generic wrapper around it. Keep in mind, when dealing with value types (like int
or double
) it will result in boxing of the keys/values (generic dictionaries don't impose boxing on value types).
这篇关于C#中的保留顺序数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!