SortedList<T> missing GetByIndex method?

B

Bruce Wood

SortedList<T> generic has most of the methods of the old SortedList
class, but it's missing a few, and one in particular that puzzles me.

Anyone know why it has no GetByIndex method?

It has IndexOfKey(), and IndexOfValue(), so you can ask it for indices
into the list. You just can't do anything with them, because it has no
method that takes an index and gives you back anything, neither an
entry, nor a key, nor a value.

The old SortedList would at least give you back the value given the
index.

So... what's the use of a sorted list if you can't get the n-th entry?

Was this an oversight? Or is it by design? Or am I blind...?
 
P

Peter Duniho

[...]
So... what's the use of a sorted list if you can't get the n-th entry?

Was this an oversight? Or is it by design? Or am I blind...?

I'll pick "blind" for 400, Alex. :)

SortedList<int> listOfInts;

...
int i = listOfInts.Values[0];

Okay, maybe it's not the BEST-documented aspect of SortedList<>. But it's
not exactly buried either. In fact, the very first code sample in the
class description page for SortedList<> demonstrates the above.

Pete
 
P

Peter Duniho

SortedList<int> listOfInts;

...
int i = listOfInts.Values[0];

And yes, my declaration of SortedList<> is wrong (as is your reference to
the non-existent generic class "SortedList<T>"...it's actually
"SortedList<TKey, TValue>" :p )...one hopes that that does not in any way
impeded your ability to get the general idea of how to index into the
values of the generic sorted list. :)
 
C

Chris Dunaway

SortedList<int> listOfInts;
...
int i = listOfInts.Values[0];

And yes, my declaration of SortedList<> is wrong (as is your reference to
the non-existent generic class "SortedList<T>"...it's actually
"SortedList<TKey, TValue>" :p )...one hopes that that does not in any way
impeded your ability to get the general idea of how to index into the
values of the generic sorted list. :)

If you use an integer as the key, you can use this syntax:

SortedList<int, string> list = new SortedList<int, string>();

....

string s = list[4];

Chris
 
P

Peter Duniho

If you use an integer as the key, you can use this syntax:

SortedList<int, string> list = new SortedList<int, string>();

...

string s = list[4];

No, you can't. That syntax will retrieve items from the list using the
value given to the [] operator as a key. The original poster wants a way
to retrieve the items by simple index within the list, not based on the
key.

Using the above, if you have a SortedList<int, string> with elements in
this order:

1, "one"
2, "two"
3, "three"
5, "five"
6, "six"

Then list[4] will throw an exception and list[3] will return the string
"three" even though that's the element at index 2. The original poster
wants to be able to (for example) use 4 as an index and retrieve the value
"six". He can do this by using the Values property: list.Values[4] (as I
mentioned).

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top