当哈希表中有这个结构时,如何从SortedList读取值?
下面是一个例子
public SortedList sl = new SortedList();
sl[test] = 1;
Hashtable ht= new Hashtable();
ht.Add("root", sl);我想读sl[test]。
发布于 2014-05-24 09:02:36
你只需做相反的事情:
SortedList sortedList = (SortedList)ht["root"];
object value = sortedList[test];发布于 2014-05-24 09:14:36
就目前情况而言,您需要将哈希表的结果转换回SortedList,然后才能使用索引器等方法,需要这种丑陋:
var result = (ht["root"] as SortedList)[test];但是,如果哈希表的所有元素都是SortedList,则可以使用泛型容器(如Dictionary )来避免转换:
var dic = new Dictionary<string, SortedList> { { "root", sl } };
result = dic["root"][test];您还可以考虑将SortedList替换为它的属对口,例如SortedList<string, int> (取决于‘test’的类型),原因是相同的。
https://stackoverflow.com/questions/23843178
复制相似问题