Accessing a Dictionary.Keys Key through a numeric index

前端 未结 15 1245
失恋的感觉
失恋的感觉 2020-12-07 13:49

I\'m using a Dictionary where the int is a count of the key.

Now, I need to access the last-inserted Key inside the Dict

15条回答
  •  不知归路
    2020-12-07 14:06

    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);
        }
    }
    

提交回复
热议问题