views:

450

answers:

2

I'm using .NET 3.5. I think I know the answer to this, but am looking for confirmation so should be a quick one!

There doesn't appear to be a generic implementation of OrderedDictionary (which is in System.Collections.Specialized namespace). Is there one that I'm missing?

I've found implementations out there to provide the functionality, but wondered if/why there isn't a generic implementation out-of-the-box and if anyone knows whether it's something in .NET 4.0?

+2  A: 

You're right. There's no generic equivalent of OrderedDictionary in the framework itself.

(That's still the case for .NET4 too, as far as I'm aware.)

LukeH
Thanks. Confirmed it quickest!
AdaTheDev
A: 

There surely is no generic version of this because it would need a generic comparer implemented. The OrderedDictionary could be ordered, because the type of objects is known through its definition and therefore a comparer for those types can be implemented directly.

Butyou can use the generic dictionary, provide an IComparer object to it and sort it like needed -> at last with extension method .OrderBy(). This flexibilty is needed, since the IComparer must be generic for the same type as the dictionary.

BeowulfOF
There are already `SortedDictionary<K,V>` and `SortedList<K,V>` types in the `System.Collections.Generic` namespace, but they're *not* equivalents of `OrderedDictionary`.
LukeH
@Luke, what are the differences?
BeowulfOF
An `OrderedDictionary` isn't sorted: if you access it by index then you're dealing with the items in their insertion order. `SortedDictionary<K,V>` and `SortedList<K,V>` are sorted on the key: if you access them by index then you're dealing with the items in their sorted order.
LukeH
Aww.. ok, thought wrong of that.
BeowulfOF