【发布时间】:2011-08-24 13:26:41
【问题描述】:
SortedList<string, int> list = new SortedList<string, int>;
list.Add("abc", 2);
list.Add("def", 3);
string index = list.GetIndex(2) ???????
Console.WriteLine(index);
我真的很困惑。如何使用索引的值获取数组的索引??
输出应该是
abc
解决方案:
使用SortedList公开的IndexOfValue方法,结合Linq扩展方法ElementAt(int index),所以:
string index = list.ElementAt(list.IndexOfValue(value)).Key;//output will be abc
---o---
这也有效
string index = list.Keys.ElementAt<string>(list.IndexOfValue(2));
【问题讨论】:
-
我和你一样困惑。什么阵法?我没有看到任何数组。
-
如果 2 和 3 只是索引引用,为什么不直接使用字符串列表,或者 int, string 的字典?
-
实际上我有一个以 ToolStripMenuItem 为索引和 Form 作为值的 SortedList。
标签: c#