通过数字索引访问 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章