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 (i.e. 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 aSortedDictionary<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 (likeintordouble) it will result in boxing of the keys/values (generic dictionaries don’t impose boxing on value types).