开发者

Return first element in SortedList in C#

I have a SortedList in C# and I want to return the first element of the lis开发者_如何学Ct. I tried using the "First" function but it didnt really work.

Can someone please tell me how to do it?


For both SortedList and SortedList<T>, you can index the Values property:

SortedList listX = new SortedList();
    
listX.Add("B", "B");
listX.Add("A", "A");

MessageBox.Show(listX.Values[0]);


Gordon Bell replied, but in the case you need to get the first key as well.

For the non generic collection:

        SortedList tmp2 = new SortedList();
        tmp2.Add("temp", true);
        tmp2.Add("a", false);
        Object mykey = tmp2.GetKey(0);
        Object myvalue = tmp2.GetByIndex(0);

For the generic collection:

        SortedList<String, Boolean> tmp = new SortedList<String, Boolean>();
        tmp.Add("temp", true);
        tmp.Add("a", false);

        String firstKey = tmp.Keys[0];
        bool firstval = tmp[firstKey];


For a non-generic SortedList, use GetByIndex:

if (list.Count > 0)
  return list.GetByIndex(0);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜