C# 如何获取字典中的键列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1276763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the list of keys in a dictionary?
提问by Athiwat Chunlakhan
I never gotten any code I tried to work.
我从来没有得到任何我试图工作的代码。
I want the keys and not the values (yet). Using another array proved to be too much work as I use remove also.
我想要键而不是值(还)。使用另一个数组被证明是太多的工作,因为我也使用 remove 。
采纳答案by Camal
List<string> keyList = new List<string>(this.yourDictionary.Keys);
回答by Marc Gravell
You should be able to just look at .Keys
:
你应该能够只看.Keys
:
Dictionary<string, int> data = new Dictionary<string, int>();
data.Add("abc", 123);
data.Add("def", 456);
foreach (string key in data.Keys)
{
Console.WriteLine(key);
}
回答by Thorarin
Marc Gravell's answer should work for you. myDictionary.Keys
returns an object that implements ICollection<TKey>
, IEnumerable<TKey>
and their non-generic counterparts.
Marc Gravell 的回答应该对你有用。myDictionary.Keys
返回一个实现 的对象ICollection<TKey>
,IEnumerable<TKey>
以及它们的非泛型对应物。
I just wanted to add that if you plan on accessing the value as well, you could loop through the dictionary like this (modified example):
我只想补充一点,如果您还计划访问该值,则可以像这样遍历字典(修改后的示例):
Dictionary<string, int> data = new Dictionary<string, int>();
data.Add("abc", 123);
data.Add("def", 456);
foreach (KeyValuePair<string, int> item in data)
{
Console.WriteLine(item.Key + ": " + item.Value);
}
回答by Dan
The question is a little tricky to understand but I'm guessing that the problem is that you're trying to remove elements from the Dictionary while you iterate over the keys. I think in that case you have no choice but to use a second array.
这个问题有点难以理解,但我猜测问题在于您在迭代键时试图从 Dictionary 中删除元素。我认为在这种情况下,您别无选择,只能使用第二个数组。
ArrayList lList = new ArrayList(lDict.Keys);
foreach (object lKey in lList)
{
if (<your condition here>)
{
lDict.Remove(lKey);
}
}
If you can use generic lists and dictionaries instead of an ArrayList then I would, however the above should just work.
如果您可以使用通用列表和字典而不是 ArrayList,那么我会,但是以上应该可以正常工作。
回答by Dan
Or like this:
或者像这样:
List< KeyValuePair< string, int > > theList =
new List< KeyValuePair< string,int > >(this.yourDictionary);
for ( int i = 0; i < theList.Count; i++)
{
// the key
Console.WriteLine(theList[i].Key);
}
回答by prem
To get list of all keys
获取所有键的列表
using System.Linq;
List<String> myKeys = myDict.Keys.ToList();
System.Linq is supported in .Net framework 3.5 or above. See the below links if you face any issue in using System.Linq
.Net framework 3.5 或更高版本支持 System.Linq。如果您在使用 System.Linq 时遇到任何问题,请参阅以下链接
Visual Studio Does not recognize System.Linq
回答by Kosmas
For a hybrid dictionary, I use this:
对于混合字典,我使用这个:
List<string> keys = new List<string>(dictionary.Count);
keys.AddRange(dictionary.Keys.Cast<string>());
回答by Joseph Wu
I often used this to get the key and value inside a dictionary: (VB.Net)
我经常用它来获取字典中的键和值:(VB.Net)
For Each kv As KeyValuePair(Of String, Integer) In layerList
Next
(layerList is of type Dictionary(Of String, Integer))
(layerList 的类型是 Dictionary(Of String, Integer))
回答by Antony Booth
I can't believe all these convoluted answers. Assuming the key is of type: string (or use 'var' if you're a lazy developer): -
我无法相信所有这些令人费解的答案。假设密钥的类型为:string(如果您是一个懒惰的开发人员,则使用“var”):-
List<string> listOfKeys = theCollection.Keys.ToList();