問題:
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