为何泛型的SortedList,提供的函数还没有非泛型的版本多
为什么泛型的SortedList,提供的函数还没有非泛型的版本多?
我自己写了一个小例子对比了一下。SortedList()有2中得到Value的方式,[]和GetByIndex,还可以通过GetKey来获得Key。
而SortedList<>()只能[]或者TryGetValue,好像还不能直接获得某个位置上的Key:
我的问题在于: SortedList<>能否直接获得,例如排序后第3个位置上元素的key和value分别是多少? 就像非泛型版的GetByIndex和GetKey那样?
谢谢。
------解决思路----------------------
...直接获得某个位置上的Key
int i = sl.Keys[2];
------解决思路----------------------
SortedList<string, int> sl = new SortedList<string, int>();
var key = sl.Keys[3];
var value = sl.Values[3];
我自己写了一个小例子对比了一下。SortedList()有2中得到Value的方式,[]和GetByIndex,还可以通过GetKey来获得Key。
而SortedList<>()只能[]或者TryGetValue,好像还不能直接获得某个位置上的Key:
SortedList list = new SortedList {{40, "kkk"}, {3, "abc"}, {2, "xyz"}, {23, "sadf"}};
Console.WriteLine(list[2]);
Console.WriteLine(list.GetByIndex(2));
Console.WriteLine(list.GetKey(2));
var sl = new SortedList<int, string>
{
{20, "abc"},
{30, "kkk"},
{4, "adsf"},
{9, "ytyyu"}
};
//Console.WriteLine(sl[2]);
string s;
sl.TryGetValue(2, out s);
if(s!=null)
Console.WriteLine(s);
我的问题在于: SortedList<>能否直接获得,例如排序后第3个位置上元素的key和value分别是多少? 就像非泛型版的GetByIndex和GetKey那样?
谢谢。
------解决思路----------------------
...直接获得某个位置上的Key
int i = sl.Keys[2];
------解决思路----------------------
SortedList<string, int> sl = new SortedList<string, int>();
var key = sl.Keys[3];
var value = sl.Values[3];