Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Why did Microsoft not provide generic implementation of OrderedDictionary?

There are a few custom implementations I've seen, including: http://www.codeproject.com/KB/recipes/GenericOrderedDictionary.aspx

But why did Microsoft not include it in the base .net library? Surely they had a reason for not building a generic.... but what is it?

Prior to posting this message, I did see: http://stackoverflow.com/questions/2629027/no-generic-implementation-of-ordereddictionary

But that just confirms that it does not exist. Not why it does not exist.

Thanks

share|improve this question
    
There's always SortedDictionary<TKey, TValue> : msdn.microsoft.com/en-us/library/f7fta44c.aspx –  Travis Gockel Nov 2 '10 at 14:51
    
Unless I misunderstand the SortedDict docs, that is not what I want. I don't want any sorting done. I just want an array that I can also access by key. Anyway, I am really wondering why MS skipped this. (Subtle issues, etc.) –  nonot1 Nov 2 '10 at 15:16
    
What's a typical use case for an ordered dictionary? I'm struggling to think of one off the top of my head. –  Carson63000 Nov 3 '10 at 2:06
    
@Carson anytime you have what is an array of data items that you also need quick random access to. To simply store in a Dict loses the ordering information, and using an array requires your to maintain your own index. –  nonot1 Nov 3 '10 at 11:30
1  
Ask Eric Lippert to read and answer your question. He's usually very agreeable to helping out with this type of question. I think you can contact him via his blog: blogs.msdn.com/b/ericlippert –  devuxer Jul 11 '11 at 23:54
show 2 more comments

3 Answers

In C# 4.0 In A Nutshell I read this:

  1. An OrderedDictionary is a combination of a HashTable and ArrayList
  2. There is no generic ArrayList
  3. "The nongeneric ArrayList class is used mainly for backward compatibility with Framework 1.x..."
  4. "An ArrayList is functionally similiar to List<object>"
  5. "Reflection is easier with a nongeneric ArrayList than a List<object>"

Conclusion?

No generic OrderedDicitionary because it's underlying construct is a (unofficially) depreciated class that has no generic version itself.

share|improve this answer
add comment

Because maintaining order prevents the O(1) lookup that IDictionary implies unless you wrap two collections (one for order, one for lookup), which makes add/remove less performant and increases memory usage. Or you could have slower lookup in exchance for less memory usage.

My guess is that there was no 'clearly better' choice here, so it didn't go into the standard library. Especially around 2.0, C# was still learning from Java's mistakes. I wouldn't be surprised if Java's 'everything and the kitchen sink' approach to collections in their standard library was viewed as something to avoid as well.

share|improve this answer
add comment

The OrderedDictionary overloads the indexing operation so that indexing with an integer N will get the item in position N, while indexing with an Object will retrieve the item coresponding to that object. If one were to create an OrderedDictionary<int, string> called myDict, and added items (1,"George") and (0,"Fred") in that order, should myDict[0] return "George" or "Fred"?

Such an issue could have been resolved by imposing a class constraint on the key type. On the other hand, much of the usefulness of generic collections stems from their ability to efficiently work with value types. Imposing a class constraint on the key type would seem a little ugly.

If the class didn't have to be CLS compliant but merely had to work with vb.net, a sensible design might have been to used named indexed properties. Thus, in the example above, myDict.ByKey[0] would have yielded "Fred", and myDict.BySequence[0] would have yielded "George". Unfortunately, languages like C# do not support named indexed properties. While one could have kludged something to allow use of the above syntax even without such properties, the unfortunate decision to wrap the fields of structures like Point and Rectangle means that for myDict.ByKey[0] = "Wally" to work, myDict.ByKey would have to return a new class object. A struct would be more efficient, but compilers would reject what looked like a write to a read-only structure (notwithstanding that the property wouldn't modify the struct returned by ByKey, but instead modify the collection to which it holds a reference).

Personally, I think a dictionary-ish object that was specified as keeping track of the insertion order would be a nice thing to have; I'd also like to have a dictionary-ish object which could easily return the key associated with a particular key (so that, e.g. if one has a case-insensitive dictionary and has added a record with a key of "GEORGE", one could ask the dictionary what key is associated with "George" without having to search through all the KeyValuePair objects returned in an enumeration.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.