views:

594

answers:

3

Hello,

I have a SorterList in C# and I want to return the first element of the list. I tried using the "First" function but it didnt really work.

Can someone please tell me how to do it?

Thanks in advance,

Greg

+4  A: 

Use GetByIndex

if (list.Count > 0)
  return list.GetByIndex(0);
Jorge Córdoba
http://msdn.microsoft.com/en-us/library/system.collections.sortedlist.getbyindex(VS.80).aspx for the english speaking minority.
Yuriy Faktorovich
Ooops, edited... thx for the link ... it was all google smart language search fault :)
Jorge Córdoba
A: 
SortedList listX = new SortedList();

listX.Add("B", "B");
listX.Add("A", "A");

MessageBox.Show(listX.Values(0));
Gordon Bell
A: 

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];
BlueTrin