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 不保留插入顺序(即有赠品中的哈希")
- 字典和列表做保留插入顺序.
- 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
定义了一条曲线,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
以任意顺序维护.在 SortedDictionary
中键和值按键的值按顺序维护 - 这与插入顺序不同.
.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屋!