0

System.Collections.DictionaryEntry is avoided for new projects, as seen in the Docs:

We don't recommend that you use the DictionaryEntry structure for new development. Instead, we recommend that you use a generic KeyValuePair<TKey,TValue> structure along with the Dictionary<TKey,TValue> class. For more information, see Non-generic collections shouldn't be used on GitHub.

However the System.Collections.Specialized.OrderedDictionary, which is not marked on its Docs page as deprecated, cannot be used with KeyValuePairs and it is required to bring the DictionaryEntrys for value access.

var pages = new OrderedDictionary()
{
    { "Index", "Home" },
    { "Policies", "Privacy Policy" },
    // ...
};

foreach (DictionaryEntry de in pages)
{
    // OK
}

foreach (KeyValuePair<string, string> pair in pages)
{
    // InvalidCastException: Unable to cast object of type 'System.Collections.DictionaryEntry' 
    //     to type 'System.Collections.Generic.KeyValuePair`2[System.String,System.String]'.
}

Given these, should we now consider OrderedDictionary as obsolete as DictionaryEntry? Or is it still OK to use?


Edit:

I am not talking about the difference of Dictionary<> and OrderedDictionary. What I want is choosing KeyValuePair instead of DictionaryEntry because the latter one is not recommended any more, but I could not find a generic version of OrderedDictionary, like Dictionary<> for Hashtable.

Henry Kwon
  • 101
  • 1
  • 3
  • 12
  • 2
    Does this answer your question? [OrderedDictionary and Dictionary](https://stackoverflow.com/questions/16694182/ordereddictionary-and-dictionary) – jazb Apr 12 '22 at 07:04
  • 1
    @Jazb Not quite, because the suggested question is about the difference of two classes, but mine is about how to use (or whether to avoid) a class with an obsolete component when its up-to-date counterpart is not accepted. – Henry Kwon Apr 12 '22 at 07:19
  • 1
    Note that it is a *recommendation*, not obsoletion, which can be marked with the `ObsoleteAttribute` but that hasn't been done here. If you truly need an `OrderedDictionary` then you have no choice but to use it. There isn't a generic variant of `OrderedDictionary`. – Damien_The_Unbeliever Apr 12 '22 at 07:42
  • You can use SortedDictionary or SortedList, if you really need access an ordered collection by index, you can create one with List+Dictionary in two ticks. – shingo Apr 12 '22 at 08:39
  • Forget xxxDictionary - what do you want to do? – Caius Jard Apr 12 '22 at 08:55

0 Answers0