tags:

views:

17174

answers:

15

I'm using a Dictionary<string, int> where the int is a count of the key.

Now, I need to access the last-inserted Key inside the Dictionary, but i do not know the name of it. The obvious attempt:

int LastCount = mydict[mydict.keys[mydict.keys.Count]];

does not work, because Dictionary.Keys does not implement a []-indexer.

I just wonder if there is any similar class? I thought about using a Stack, but that only stores a string. I could now create my own struct and then use a Stack<MyStruct>, but I wonder if there is another alternative, essentially a Dictionary that implements an []-indexer on the Keys?

+3  A: 

You could always do this:

string[] temp = new string[mydict.count];
mydict.Keys.CopyTo(temp, 0)
int LastCount = mydict[temp[mydict.count - 1]]

But I wouldn't recommend it. There's no guarantee that the last inserted key will be at the end of the array. The ordering for Keys on MSDN is unspecified, and subject to change. In my very brief test, it does seem to be in order of insertion, but you'd be better off building in proper bookkeeping like a stack--as you suggest (though I don't see the need of a struct based on your other statements)--or single variable cache if you just need to know the latest key.

Patrick
A: 

I don't know if this would work because I'm pretty sure that the keys aren't stored in the order they are added, but you could cast the KeysCollection to a List and then get the last key in the list... but it would be worth having a look.

The only other thing I can think of is to store the keys in a lookup list and add the keys to the list before you add them to the dictionary... it's not pretty tho.

lomaxx
+1  A: 

I think you can do something like this, the syntax might be wrong, havent used C# in a while To get the last item

Dictionary<string, int>.KeyCollection keys = mydict.keys;
string lastKey = keys.Last();

or use Max instead of Last to get the max value, I dont know which one fits your code better.

Juan
A: 

@Juan: there is no .Last() method on the KeyCollection

lomaxx
should be a comment?
Svish
A: 

@lomaxx I didnt test the code, but the method is documented on MSDN maybe its another version the framework?

Juan
+1  A: 

I agree with the second part of Patrick's answer. Even if in some tests it seems to keep insertion order, the documentation (and normal behavior for dictionaries and hashes) explicitly states the ordering is unspecified. You're just asking for trouble depending on the ordering of the keys. Add your own bookkeeping (as Patrick said, just a single variable for the last added key) to be sure. Also, don't be tempted by all the methods such as Last and Max on the dictionary as those are probably in relation to the key comparator (I'm not sure about that).

Stephen Pellicer
A: 

The way you worded the question leads me to believe that the int in the Dictionary contains the item's "position" on the Dictionary. Judging from the assertion that the keys aren't stored in the order that they're added, if this is correct, that would mean that keys.Count (or .Count - 1, if you're using zero-based) should still always be the number of the last-entered key?

If that's correct, is there any reason you can't instead use Dictionary<int, string> so that you can use mydict[ mydict.Keys.Count ]?

Jeremy Privett
+2  A: 

Why don't you just extend the dictionary class to add in a last key inserted property. Something like the following maybe?

public class ExtendedDictionary : Dictionary { private int lastKeyInserted = -1;

public int LastKeyInserted
{
    get { return lastKeyInserted; }
    set { lastKeyInserted = value; }
}

public void AddNew(string s, int i)
{
    lastKeyInserted = i;

    base.Add(s, i);
}

}

Calanus
A: 

Thanks for all the answers! Gambling on the insert order of keys is indeed gonna be a game i'm not gonna play.

I think I keep the Dictionary, but additionally use a List or Stack that holds the order of Keys. The amount of Entries is not that high (less than 10), so the "overhead" is non-existant.

Michael Stum
+12  A: 

You can use an OrderedDictionary.

Represents a collection of key/value pairs that are accessible by the key or index.

Andrew Peters
A: 

I'm curious as to why you need to access the last added key. Are you sure it can't be achieved by overloading the Add methods?

ICR
+2  A: 

A Dictionary is a Hash Table, so you have no idea the order of insertion!

if you want to know the last inserted key i would suggest extending the Dictionary to include a LastKeyInserted value.

ex:

public MyDictionary<K, T> : IDictionary<K, T>
{
    private IDictionary<K, T> _InnerDictionary;

    public K LastInsertedKey { get; set; }

    public MyDictionary()
    {
        _InnerDictionary = new Dictionary<K, T>();
    }

    #region Implementation of IDictionary

    public void Add(KeyValuePair<K, T> item)
    {
        _InnerDictionary.Add(item);
        LastInsertedKey = item.Key;

    }

    public void Add(K key, T value)
    {
        _InnerDictionary.Add(key, value);
        LastInsertedKey = key;
    }

    .... rest of IDictionary methods

    #endregion

}

you will run into problems however when you use .Remove() so to overcome this you will have to keep an ordered list of the keys inserted.

Hope this helps.

A: 

how about the NameValueCollection object?

BrokeMyLegBiking
A: 

In case you decide to use dangerous code that is subject to breakage, this extension function will fetch a key from a Dictionary according to its internal indexing (which for Mono and .NET currently appears to be in the same order as you get by enumerating the Keys property).

It is much preferable to use Linq: dict.Keys.ElementAt(i), but I don't know if that function is smart enough to not iterate O(N). The following is O(1) but with a reflection performance penalty.

using System;
using System.Collections.Generic;
using System.Reflection;

public static class Extensions
{
    public static TKey KeyByIndex<TKey,TValue>(this Dictionary<TKey, TValue> dict, int idx)
    {
        Type type = typeof(Dictionary<TKey, TValue>);
        FieldInfo info = type.GetField("entries", BindingFlags.NonPublic | BindingFlags.Instance);
        if (info != null)
        {
            // .NET
            Object element = ((Array)info.GetValue(dict)).GetValue(idx);
            return (TKey)element.GetType().GetField("key", BindingFlags.Public | BindingFlags.Instance).GetValue(element);
        }
        // Mono:
        info = type.GetField("keySlots", BindingFlags.NonPublic | BindingFlags.Instance);
        return (TKey)((Array)info.GetValue(dict)).GetValue(idx);
    }
};
Glenn Slayden