通过数字索引访问Dictionary.Keys关键 [英] Accessing a Dictionary.Keys Key through a numeric index

查看:1327
本文介绍了通过数字索引访问Dictionary.Keys关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是词典<字符串,整数> 其中 INT 是关键的计数。

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]];

不起作用,因为 Dictionary.Keys 没有实现[] -indexer。

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

我只是不知道是否有任何类似的类?我想过使用堆栈,但只存储一个字符串。我现在就可以创建自己的结构,然后用堆栈&LT; MYSTRUCT&GT; ,但我不知道是否有另一种选择,基本上是在实现了[] -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?

推荐答案

它非常简单。但是,你需要使用LINQ。

Its very simple. But you need use linq.

int LastCount = mydict.Keys.ElementAt(mydict.Count -1);

这篇关于通过数字索引访问Dictionary.Keys关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆