字典支持通过键访问元素。 列表支持通过索引访问。 有没有同时支持键和索引访问的数据结构?
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("abc", "def");
d.Add("ghi", "ghi");
d.Add("abcd", "abcd");
d.Add("how", "howis");
foreach (KeyValuePair<string, string> kv in d)
Console.WriteLine(kv.Key);
输出是
abc
ghi
abcd
how
因此,项目的出现顺序与添加的顺序相同。这是否意味着字典按顺序维护项目?如果是,我们可以通过索引访问它们吗?如果没有,是否有支持通过索引和键访问元素的此类可用集合?
最佳答案
我认为OrderedDictionary <罢工> 或 NameValueCollection 就是您要找的。p>
关于c# - 支持通过索引和键访问的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16420003/