c#顺序保存数据结构 [英] c# order preserving data structures

查看:132
本文介绍了c#顺序保存数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

奇怪的是,MSDN没有关于数据结构的保存属性的信息。所以我一直在假设:




  • Hashtable和Hashset不保留插入顺序(也称为哈希)一个赠品)

  • 字典和列表DO保留插入顺序。



从这个我推断如果我有一个字典< double,double> foo 定义一个曲线,foo.Keys.ToList()和foo.Values.ToList()将给我一个有序的列表,该曲线的范围和域不会混乱? p>

解决方案

您不应该期望常规的 字典< TKey,TValue> 以任何顺序进行维护。在 SortedDictionary< TKey,TValue> 按键的值按键保持顺序 - 这与插入顺序不一样。



.NET框架中唯一保留插入顺序的内置字典是 System.Collections.Specialized.OrderedDictionary 。不幸的是,这个类不是通用的 - 但是,编写一个通用的包装器是不是很难。请记住,在处理价值类型(如 int double )时,会导致键/值(通用字典不会对值类型施加拳击)。


Oddly enough, MSDN has no information on the order preserving properties of data structures. So I've been making the assumption that:

  • Hashtable and Hashset do not preserve the insertion order (aka the "hash" in there is a giveaway)
  • Dictionary and List DO preserve the insertion order.

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?

解决方案

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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆