System.Collections Namespace
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.
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.
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 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 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.
Operations on a SortedList tend to be slower than operations on a Hashtable 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.
A key cannot be a null reference (Nothing in Visual Basic), but a value can be a null reference (Nothing).
Indexes in this collection are zero-based.
The foreach statement of the C# language (for each ...next in Visual
Basic) requires the type of each element in the collection. Since each element
of a collection based on IDictionary
is a key-and-value pair, the element type is not the type of the key or the type
of the value. Instead, the element type is DictionaryEntry. For example:
foreach ( DictionaryEntry myEntry in myHashtable ) {
...
}
for each myEntry as DictionaryEntry in myHashtable
...
next |
|
C# |
VB |
The following example shows how to create and initialize a SortedList and how to print out its keys and values.
using System;
using System.Collections;
public class SamplesSortedList {
public static void Main() {
// Creates and initializes a new SortedList.
SortedList mySortedList = new SortedList();
mySortedList.Add ("First", "Hello");
mySortedList.Add ("Second", "World");
mySortedList.Add ("Third", "!");
// Displays the properties and values of the SortedList.
Response.Write ("mySortedList" );
Response.Write ("Count: " + mySortedList.Count + "<br>");
Response.Write ("Capacity: " + mySortedList.Capacity + "<br>");
Response.Write ("Keys and Values:");
PrintKeysAndValues ( mySortedList );
}
public static void PrintKeysAndValues ( SortedList myList ) {
Response.Write ("Key, Value") {
for ( int i = 0; i < myList.Count; i++ ) {
Response.Write (myList.GetKey(i) + ", " + myList.GetByIndex(i));
}
Response.Write ("<br>");
}
}
Imports System
Imports System.Collections
Public Class SamplesSortedList
Public Shared Sub Main()
' Creates and initializes a new SortedList.
Dim mySortedList As New SortedList()
mySortedList.Add ("First", "Hello")
mySortedList.Add ("Second", "World")
mySortedList.Add ("Third", "!")
' Displays the properties and values of the SortedList.
Response.Write ("mySortedList")
Response.Write ("Count: " & mySortedList.Count & "<br>")
Response.Write ("Capacity: " & mySortedList.Capacity & "<br>")
Response.Write ("Keys and Values:")
PrintKeysAndValues (mySortedList)
End Sub
Public Shared Sub PrintKeysAndValues (myList As SortedList)
Response.Write ("Key", "Value")
Dim i As Integer
For i = 0 To myList.Count - 1
Response.Write (myList.GetKey(i) & ", " & myList.GetByIndex(i))
Next i
Response.Write ("<br>")
End Sub
End Class |
|
C# |
VB |