win*_*aed 17 .net c# dictionary
怀疑我的大脑今天不工作 - 我需要提取密钥列表等:
Dictionary<string, MyClass> myDict;
List<String> myKeys = myDict.Keys;
Run Code Online (Sandbox Code Playgroud)
第二行无法编译,因为Keys属性返回"KeyCollection"类而不是键对象的列表<>.
Qui*_*son 32
使用LINQ,您可以执行以下操作...
List<String> myKeys = myDict.Keys.ToList();
Run Code Online (Sandbox Code Playgroud)
但是,根据您的目标是键(选择性枚举等),使用密钥集合而不是转换为列表可能更有意义.
Jar*_*dek 22
KeyCollection实现了IEnumerable
接口.
您可以使用扩展方法将其转换为列表.
List<String> myKeys = myDict.Keys.ToList();
Run Code Online (Sandbox Code Playgroud)
或者使用不同的构造函数:
List<String> myKeys = new List<String>(myDict.Keys);
Run Code Online (Sandbox Code Playgroud)