When I have SortedDictionary<TK, TV> in .NET and I want to enumerate it as ICollection<KeyValuePair<TK, TV>> does it enumerate in expected order?
That is KeyValuePair<TK, TV> with lowest key is returned as first, folloved by KeyValuePair<TK, TV> with second lowest key etc.?
Note: Only answer backed up by reference will be accepted.
Yes definitely, although you are going to find it very hard to find documentation that clarifies this precisely.
Although the documentation for each of the four
GetEnumeratoroverloads on this type make vague statements about returning “an enumerator that iterates through a collection”, it is obvious enough that they should produce equivalent (sorted by key) sequences; remember that a sorted-dictionary is meant to “represent a collection of key/value pairs that are sorted on the key.” It would be highly unintuitive and confusing for users if a collection behaved completely differently (i.e. with a different enumeration order) between aforeachloop and a LINQ to Objects query, for example.The best I can do is provide you with the implementations of the two
GetEnumeratormethods you appear to be interested in (as of .NET 4.0). They are identical – they return an instance of the nestedEnumeratortype, with the same arguments for its constructor. The only difference is the boxing of the struct-type in the second overload:In fact, the only
GetEnumeratoroverload that has a slightly different implementation is theIDictionary.GetEnumeratormethod. This changes an argument to the constructor-call such that the resulting enumerator producesDictionaryEntryinstances rather thanKeyValuePair<,>instances. Of course, the enumeration order will still be the same as with the other overloads.