Can a SortedList be accessed with a negative index?

  • Thread starter Thread starter Jen
  • Start date Start date
J

Jen

I'm looking at some old C# code and I found a line where a SortedList is
being accessed with a negative index, like so:

groups[-1];

My C# skills must be getting rusty. How can that work? What does it do?
 
Jen said:
I'm looking at some old C# code and I found a line where a SortedList is
being accessed with a negative index, like so:

groups[-1];

My C# skills must be getting rusty. How can that work? What does it do?
A SortedList is actually a dictionary -- the name is particularly poorly
chosen because the class is not very suitable for an *actual* sorted list
either.

The keys of a SortedList can be arbitrary objects; in this case, integers.
Those don't have to be nonnegative (or consecutive).

What you're thinking of is that the items can *also* be accessed by ordinal
[0..Count), but that's through the .GetByIndex() method.
 

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

Back
Top