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 KeyValuePair
s and it is required to bring the DictionaryEntry
s 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
.