我只想要字典的键而不是值。
我还没能得到任何代码来做这个。使用另一个数组被证明是太多的工作,因为我使用删除也。
我如何在字典中获得键的列表?
我只想要字典的键而不是值。
我还没能得到任何代码来做这个。使用另一个数组被证明是太多的工作,因为我使用删除也。
我如何在字典中获得键的列表?
当前回答
我经常使用这个来获取字典中的键和值:
For Each kv As KeyValuePair(Of String, Integer) In layerList
Next
(layerList类型为Dictionary(of String, Integer))
其他回答
你应该可以只看。
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);
}
对于混合字典,我使用这个:
List<string> keys = new List<string>(dictionary.Count);
keys.AddRange(dictionary.Keys.Cast<string>());
我真不敢相信这些令人费解的答案。假设键的类型是:string(或者使用'var'如果你是一个懒惰的开发人员):-
List<string> listOfKeys = theCollection.Keys.ToList();
List<string> keyList = new List<string>(this.yourDictionary.Keys);
我经常使用这个来获取字典中的键和值:
For Each kv As KeyValuePair(Of String, Integer) In layerList
Next
(layerList类型为Dictionary(of String, Integer))