24

How do I get the key and value of item from OrderedDictionary by index?

1
  • Consider changing the accepted answer. Jan 20, 2019 at 11:19

3 Answers 3

51
orderedDictionary.Cast<DictionaryEntry>().ElementAt(index);
4
  • 2
    using using System.Linq;
    – testing
    Sep 12, 2014 at 9:56
  • With this code you get the element. But how do I get the key and the value as in the SO question?
    – testing
    Sep 12, 2014 at 10:02
  • 8
    orderedDictionary.Cast<DictionaryEntry>().ElementAt(index).Key.ToString();
    – testing
    Sep 29, 2014 at 8:58
  • 3
    + 1 This should be the accepted answer actually. Verify here - referencesource.microsoft.com/#System/compmod/system/… Jan 2, 2019 at 19:10
8

There is not a direct built-in way to do this. This is because for an OrderedDictionary the index is the key; if you want the actual key then you need to track it yourself. Probably the most straightforward way is to copy the keys to an indexable collection:

// dict is OrderedDictionary
object[] keys = new object[dict.Keys.Count];
dict.Keys.CopyTo(keys, 0);
for(int i = 0; i < dict.Keys.Count; i++) {
    Console.WriteLine(
        "Index = {0}, Key = {1}, Value = {2}",
        i,
        keys[i],
        dict[i]
    );
}

You could encapsulate this behavior into a new class that wraps access to the OrderedDictionary.

4
  • 1
    I did same but see once: OrderedDictionary list = OrderItems; object strKey = list[e.OldIndex]; DictionaryEntry dicEntry = new DictionaryEntry(); foreach (DictionaryEntry DE in list) { if (DE.Value == strKey) { dicEntry.Key = DE.Key; dicEntry.Value = DE.Value; } }
    – Red Swan
    Feb 10, 2010 at 4:47
  • @RedSwan: Where is the index?
    – testing
    Sep 12, 2014 at 10:10
  • 5
    The index is definitely not the key - those are necessarily distinct constructs in an OrderedDictionary.
    – Conrad
    Feb 26, 2015 at 19:14
  • 2
    Downvoted because the index is not the key, as demonstrated in the answer by @martin-r-l
    – Justin
    Apr 1, 2015 at 14:05
2

I created some extension methods that get the key by index and the value by key using the code mentioned earlier.

public static T GetKey<T>(this OrderedDictionary dictionary, int index)
{
    if (dictionary == null)
    {
        return default(T);
    }

    try
    {
        return (T)dictionary.Cast<DictionaryEntry>().ElementAt(index).Key;
    }
    catch (Exception)
    {
        return default(T);
    }
}

public static U GetValue<T, U>(this OrderedDictionary dictionary, T key)
{
    if (dictionary == null)
    {
        return default(U);
    }

    try
    {
        return (U)dictionary.Cast<DictionaryEntry>().AsQueryable().Single(kvp => ((T)kvp.Key).Equals(key)).Value;
    }
    catch (Exception)
    {
        return default(U);
    }
}
1
  • 3
    If your intent is to return default values if the targeted index/key is not in the dictionary you have chosen an expensive way to do it. Exceptions are very expensive relative to normal control-flow constructs like if/else. It would be much better to check for out-of-bounds indices and non-existing keys yourself than to rely on an exception happening.
    – Odrade
    Jul 2, 2015 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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