投了hashtable.Keys进入名单,LT; INT>或者其他的IEnumerable< INT> [英] Cast a hashtable.Keys into List<int> or other IEnumerable<int>
问题描述
我知道,我还有其他的选择,例如我可以保持按键的单独列表。请不要建议其他选项。我只是想知道如果我能顺利完成这件事。请不要问我,我想有什么问题要解决,或类似的东西。这是一个纯粹的,简单的CS问题。
我想知道是否有人知道的方式从的Hashtable $拿钥匙C $ C>扔一个
列表< INT>
或一些其他类型的的IEnumerable< INT>
(给定当然,我的钥匙其实都是整数)
由于我没有问题做到这一点:
的foreach(int类型hashtable.Keys键)
为什么?这给我的错误
(列表< INT>)hashtable.Keys
如果您有可用,你可以做LINQ扩展方法如下。
列表< INT>键= hashtable.Keys.Cast< INT>()了ToList()。
同时
列表与LT; INT>键= hashtable.Keys.OfType< INT>()了ToList()。
可能因自动装箱工作。
I know, I have other options, e.g. I could maintain a separate list of keys. Please don't suggest other options. I simply want to know if I can pull this off. Please don't ask me what problem I'm trying to solve, or anything like that. This is a pure and simple CS question.
I want to know if anyone knows of a way to take the keys from a Hashtable
and cast them into a List<int>
or some other type of IEnumerable<int>
(given of course that my keys are in fact integers).
Given that I can do this with no problem:
foreach (int key in hashtable.Keys)
Why does this give me errors?
(List<int>)hashtable.Keys
If you have LINQ extension methods available you can do the following..
List<int> keys = hashtable.Keys.Cast<int>().ToList();
Also
List<int> keys = hashtable.Keys.OfType<int>().ToList();
might work depending on automatic boxing
这篇关于投了hashtable.Keys进入名单,LT; INT>或者其他的IEnumerable< INT>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!