Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index.
See Also: SortedList Members
For the generic version of this collection, see SortedList`2.
A SortedList object internally maintains two arrays to store the elements of the list; that is, one array for the keys and another array for the associated values. Each element is a key/value pair that can be accessed as a DictionaryEntry object. A key cannot be null, but a value can be.
The capacity of a SortedList object is the number of elements the SortedList can hold. As elements are added to a SortedList, the capacity is automatically increased as required through reallocation. The capacity can be decreased by calling SortedList.TrimToSize or by setting the SortedList.Capacity property explicitly.
For very large SortedList objects, you can increase the maximum capacity to 2 billion elements on a 64-bit system by setting the enabled attribute of the gcAllowVeryLargeObjects configuration element to true in the run-time environment.
The elements of a SortedList object are sorted by the keys either according to a specific IComparer implementation specified when the SortedList is created or according to the IComparable implementation provided by the keys themselves. In either case, a SortedList does not allow duplicate keys.
The index sequence is based on the sort sequence. When an element is added, it is inserted into SortedList in the correct sort order, and the indexing adjusts accordingly. When an element is removed, the indexing also adjusts accordingly. Therefore, the index of a specific key/value pair might change as elements are added or removed from the SortedList object.
Operations on a SortedList object tend to be slower than operations on a Hashtable object because of the sorting. However, the SortedList offers more flexibility by allowing access to the values either through the associated keys or through the indexes.
Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
The foreach statement of the C# language (for each in Visual Basic) requires the type of each element in the collection. Since each element of the SortedList object is a key/value pair, the element type is not the type of the key or the type of the value. Rather, the element type is DictionaryEntry. For example:
code reference: Classic SortedList Example#2
The foreach statement is a wrapper around the enumerator, which allows only reading from, not writing to, the collection.