C#为了preserving数据结构 [英] c# order preserving data structures
问题描述
奇怪的是,MSDN对数据结构的顺序preserving性质的任何信息。所以我一直在做的假设是:
Oddly enough, MSDN has no information on the order preserving properties of data structures. So I've been making the assumption that:
- Hashtable和做的Hashset没有preserve插入顺序(也称为哈希中有一个赠品)
- 词典和List DO preserve的插入顺序。
此我推断出,如果我有一个词典<双层,双>富
定义曲线,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?
推荐答案
您不应该期望无论是键或值在普通的<一个href=\"http://msdn.microsoft.com/en-us/library/xfhwa508.aspx\"><$c$c>Dictionary<TKey,TValue>$c$c>以任何顺序来进行维护。在一<一href=\"http://msdn.microsoft.com/en-us/library/f7fta44c.aspx\"><$c$c>SortedDictionary<TKey,TValue>$c$c>键和值,以便通过该键的值保持 - 这是不一样的插入顺序的
You should NOT expect either the keys or values in a regular Dictionary<TKey,TValue>
to be maintained in any order. In a SortedDictionary<TKey,TValue>
the keys and values are maintained in order by the value of the key - this is not the same as insertion order.
在.NET框架的唯一内置字典,preserves插入顺序是<一个href=\"http://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary.aspx\"><$c$c>System.Collections.Specialized.OrderedDictionary$c$c>.不幸的是,这个类是不是通用的 - 但是,它并不十分难写它周围的一个通用的包装。值类型打交道时,请记住,(如 INT
或双击
),这将导致按键拳击/值(通用词典勿施于值类型拳)。
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#为了preserving数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!