SortedList class
Contents |
Description
The SortedList class represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index. A SortedList is a hybrid between a Hashtable and an Array. When an element is accessed by its key using the Item indexer property, it behaves like a Hashtable. When an element is accessed by its index using GetByIndex or SetByIndex, it behaves like an Array.
Members
Here are some of the common members of the SortedList class. For a complete listing see the MSDN Reference at the bottom of the page.
Properties
-
Capacity- Gets or sets the capacity of aSortedListobject.
-
Count- Gets the number of elements contained in aSortedListobject.
-
IsFixedSize- Gets a value indicating whether aSortedListobject has a fixed size. A collection with a fixed size does not allow the addition or removal of elements after the collection is created, but does allow the modification of existing elements.
-
Item- Gets and sets the value associated with a specific key in aSortedListobject.
-
Keys- Gets the keys in aSortedListobject.
-
Values- Gets the values in aSortedListobject.
Methods
-
Add(key,value)- Adds an element with the specifiedkeyandvalueto aSortedListobject. Thevaluecan be a null reference.
IfCountalready equalsCapacity, the capacity of theSortedListobject is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.
You can also use the Item property to add new elements by setting the value of a key that does not exist in the SortedList object (for example, myCollection["myNonexistentKey"] = myValue). However, if the specified key already exists in the SortedList, setting the Item property overwrites the old value. In contrast, the Add method does not modify existing elements.
Characteristics Of A SortedList
A SortedList 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-and-value pair that can be accessed as a DictionaryEntry object. A key cannot be a null reference, but a value can be.
The Sorting Of Elements
The capacity of a SortedList is the number of elements that the list 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 TrimToSize or by setting the Capacity property explicitly.
The elements of a SortedList 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 Sorting Of Indexes
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-and-value pair might change as elements are added or removed from the SortedList.
Code Examples
Adding Items To A Sorted List
mySortedList.Add( "First", "Hello" ); mySortedList.Add( "Second", "World" ); mySortedList.Add( "Third", "!" );
MSDN References
|

