通過數字索引訪問 Dictionary.Keys 鍵 - Accessing a Dictionary.Keys Key through a numeric index

問題:

I'm using a Dictionary<string, int> where the int is a count of the key.我正在使用Dictionary<string, int> ,其中int是鍵的計數。

Now, I need to access the last-inserted Key inside the Dictionary, but I do not know the name of it.現在,我需要訪問 Dictionary 中最後插入的 Key,但我不知道它的名稱。 The obvious attempt:明顯的嘗試:

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

does not work, because Dictionary.Keys does not implement a []-indexer.不起作用,因爲Dictionary.Keys沒有實現 []-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?我現在可以創建自己的結構,然後使用Stack<MyStruct> ,但我想知道是否還有另一種選擇,本質上是一個在鍵上實現 []-indexer 的字典?


解決方案:

參考一: https://en.stackoom.com/question/16B
參考二: https://stackoom.com/question/16B
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章