[Serializable] |
A SortedList can safely support multiple readers concurrently, as long as the collection is not modified. To guarantee the thread safety of the SortedList, all operations must be done through the wrapper returned by the SortedList.Synchronized method.
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
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 SortedList.TrimToSize or by setting the SortedList.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 null, but a value can be null.
Indexes in this collection are zero-based.
The foreach statement of the C# language requires the type of each element in the collection. Since each element of the Hashtable 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) {...}
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add("First", "Hello"); mySL.Add("Second", "World"); mySL.Add("Third", "!"); // Displays the properties and values of the SortedList. Console.WriteLine( "mySL" ); Console.WriteLine( " Count: {0}", mySL.Count ); Console.WriteLine( " Capacity: {0}", mySL.Capacity ); Console.WriteLine( " Keys and Values:" ); PrintKeysAndValues( mySL ); } public static void PrintKeysAndValues( SortedList myList ) { Console.WriteLine( "\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList.Count; i++ ) { Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) ); } Console.WriteLine(); } } /* This code produces the following output. mySL Count: 3 Capacity: 16 Keys and Values: -KEY- -VALUE- First: Hello Second: World Third: ! */
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Initializes a new instance of the SortedList class that is empty, has the default initial capacity and is sorted according to the IComparable interface implemented by each key added to the SortedList. |
ctor #2 | Overloaded:.ctor(IComparer comparer) Initializes a new instance of the SortedList class that is empty, has the default initial capacity and is sorted according to the specified IComparer interface. |
ctor #3 | Overloaded:.ctor(IDictionary d) Initializes a new instance of the SortedList class that contains elements copied from the specified dictionary, has the same initial capacity as the number of elements copied and is sorted according to the IComparable interface implemented by each key. |
ctor #4 | Overloaded:.ctor(int initialCapacity) Initializes a new instance of the SortedList class that is empty, has the specified initial capacity and is sorted according to the IComparable interface implemented by each key added to the SortedList. |
ctor #5 | Overloaded:.ctor(IComparer comparer, int capacity) Initializes a new instance of the SortedList class that is empty, has the specified initial capacity and is sorted according to the specified IComparer interface. |
ctor #6 | Overloaded:.ctor(IDictionary d, IComparer comparer) Initializes a new instance of the SortedList class that contains elements copied from the specified dictionary, has the same initial capacity as the number of elements copied and is sorted according to the specified IComparer interface. |
Capacity | Read-write Gets or sets the capacity of the SortedList. |
Count | Read-only Gets the number of elements contained in the SortedList. |
IsFixedSize | Read-only Gets a value indicating whether the SortedList has a fixed size. |
IsReadOnly | Read-only Gets a value indicating whether the SortedList is read-only. |
IsSynchronized | Read-only Gets a value indicating whether access to the SortedList is synchronized (thread-safe). |
Item | Read-write Gets and sets the value associated with a specific key in the SortedList. |
Keys | Read-only Gets the keys in the SortedList. |
SyncRoot | Read-only Gets an object that can be used to synchronize access to the SortedList. |
Values | Read-only Gets the values in the SortedList. |
Add | Adds an element with the specified key and value to the SortedList. |
Clear | Removes all elements from the SortedList. |
Clone | Creates a shallow copy of the SortedList. |
Contains | Determines whether the SortedList contains a specific key. |
ContainsKey | Determines whether the SortedList contains a specific key. |
ContainsValue | Determines whether the SortedList contains a specific value. |
CopyTo | Copies the SortedList elements to a one-dimensional Array instance at the specified index. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetByIndex | Gets the value at the specified index of the SortedList. |
GetEnumerator | Returns an IDictionaryEnumerator that can iterate through the SortedList. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetKey | Gets the key at the specified index of the SortedList. |
GetKeyList | Gets the keys in the SortedList. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
GetValueList | Gets the values in the SortedList. |
IndexOfKey | Returns the zero-based index of the specified key in the SortedList. |
IndexOfValue | Returns the zero-based index of the first occurrence of the specified value in the SortedList. |
Remove | Removes the element with the specified key from SortedList. |
RemoveAt | Removes the element at the specified index of SortedList. |
SetByIndex | Replaces the value at a specific index in the SortedList. |
Synchronized | Returns a synchronized (thread-safe) wrapper for the SortedList. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
TrimToSize | Sets the capacity to the actual number of elements in the SortedList. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
IEnumerable.GetEnumerator | Returns an IEnumerator that can iterate through the SortedList. |
Hierarchy:
public SortedList(); |
If the number of elements added to the list reaches the current capacity, the capacity is automatically doubled.
Each key must implement the IComparable interface to be capable of comparisons with every other key in the SortedList. The elements are sorted according to the IComparable implementation of each key added to the SortedList.
public SortedList( |
comparer
-or-
null to use the IComparable implementation of each key.
The IComparer implementation to use when comparing keys.-or-
null to use the IComparable implementation of each key.
If the number of elements added to the list reaches the current capacity, the capacity is automatically doubled.
The elements are sorted according to the specified IComparer implementation. If comparer is null, the IComparable implementation of each key is used; therefore, each key must implement the IComparable interface to be capable of comparisons with every other key in the SortedList.
public SortedList( |
d
Exception Type | Condition |
---|---|
ArgumentNullException | d is null. |
InvalidCastException | One or more elements in d do not implement the IComparable interface. |
Each key must implement the IComparable interface to be capable of comparisons with every other key in the SortedList. The elements are sorted according to the IComparable implementation of each key added to the SortedList.
A Hashtable is an example of an IDictionary implementation that can be passed to this constructor. The new SortedList contains a copy of the keys and values stored in the Hashtable.
public SortedList( |
initialCapacity
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | initialCapacity is less than zero. |
If the number of elements added to the list reaches the current capacity, the capacity is automatically doubled. Therefore, if the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the SortedList.
Each key must implement the IComparable interface to be capable of comparisons with every other key in the SortedList. The elements are sorted according to the IComparable implementation of each key added to the SortedList.
comparer
-or-
null to use the IComparable implementation of each key.
The IComparer implementation to use when comparing keys.-or-
null to use the IComparable implementation of each key.
capacity
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | initialCapacity is less than zero. |
If the number of elements added to the list reaches the current capacity, the capacity is automatically doubled. Therefore, if the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the SortedList.
The elements are sorted according to the specified IComparer implementation. If comparer is null, the IComparable implementation of each key is used; therefore, each key must implement the IComparable interface to be capable of comparisons with every other key in the SortedList.
public SortedList( |
d
comparer
-or-
null to use the IComparable implementation of each key.
The IComparer implementation to use when comparing keys.-or-
null to use the IComparable implementation of each key.
Exception Type | Condition |
---|---|
ArgumentNullException | d is null. |
InvalidCastException | comparer is null, and one or more elements in d do not implement the IComparable interface. |
The elements are sorted according to the specified IComparer implementation. If comparer is null, the IComparable implementation of each key is used; therefore, each key must implement the IComparable interface to be capable of comparisons with every other key in the SortedList.
A Hashtable is an example of an IDictionary implementation that can be passed to this constructor. The new SortedList contains a copy of the keys and values stored in the Hashtable.
public virtual int Capacity {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The value assigned is less than the current number of elements in the SortedList. |
When SortedList.Capacity is set, the internal arrays are reallocated to accommodate the new capacity.
SortedList.TrimToSize can also be used to reduce the capacity of the SortedList.
public virtual int Count {get;}
|
public virtual bool IsFixedSize {get;}
|
public virtual bool IsReadOnly {get;}
|
public virtual bool IsSynchronized {get;}
|
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
The following code example shows how to lock the collection using the SortedList.SyncRoot during the entire enumeration:
SortedList myCollection = new SortedList(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( 0, "zero" ); mySL.Add( 1, "one" ); mySL.Add( 2, "two" ); mySL.Add( 3, "three" ); mySL.Add( 4, "four" ); // Creates a synchronized wrapper around the SortedList. SortedList mySyncdSL = SortedList.Synchronized( mySL ); // Displays the sychronization status of both SortedLists. Console.WriteLine( "mySL is {0}.", mySL.IsSynchronized ? "synchronized" : "not synchronized" ); Console.WriteLine( "mySyncdSL is {0}.", mySyncdSL.IsSynchronized ? "synchronized" : "not synchronized" ); } } /* This code produces the following output. mySL is not synchronized. mySyncdSL is synchronized. */
public virtual object this[object key] {get; set;}
|
key
Exception Type | Condition |
---|---|
ArgumentNullException | key is null. |
NotSupportedException | The property is set and the SortedList is read-only. -or- The property is set, key does not exist in the collection, and the SortedList has a fixed size. |
InvalidOperationException | The comparer throws an exception. |
A value in the SortedList can be null. To distinguish between a null that is returned because the specified key is not found and a null that is returned because the value of the specified key is null, use the SortedList.Contains method or the SortedList.ContainsKey method to determine if the key exists in the list.
This property provides the ability to access a specific element in the collection by using the following syntax:
myCollection[key]
.
When setting this property, if the specified key already exists in the SortedList, the value is replaced; otherwise, a new element is created. In contrast, the SortedList.Add method does not modify existing elements.
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.
public virtual ICollection Keys {get;}
|
The elements of the ICollection are sorted in the same order as the keys of the SortedList.
Similar to SortedList.GetKeyList, but returns an ICollection instead of an IList.
public virtual object SyncRoot {get;}
|
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
The following code example shows how to lock the collection using the SortedList.SyncRoot during the entire enumeration:
SortedList myCollection = new SortedList(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
public virtual ICollection Values {get;}
|
The elements of the ICollection are sorted in the same order as the values of the SortedList.
Similar to SortedList.GetValueList, but returns an ICollection instead of an IList.
key
value
Exception Type | Condition |
---|---|
ArgumentNullException | key is null. |
ArgumentException | An element with the specified key already exists in the SortedList. -or- The SortedList is set to use the IComparable interface, and key does not implement the IComparable interface. |
NotSupportedException | The SortedList is read-only. -or- The SortedList has a fixed size. |
InvalidOperationException | The comparer throws an exception. |
The SortedList.Item property can also be used to add new elements by setting the value of a key that does not exist in the SortedList. For example:
myCollection["myNonexistentKey"] = myValue
. However, if the specified key already exists in the SortedList, setting the SortedList.Item property overwrites the old value. In contrast, the SortedList.Add method does not modify existing elements.
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.
A key cannot be null, but a value can.
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( "one", "The" ); mySL.Add( "two", "quick" ); mySL.Add( "three", "brown" ); mySL.Add( "four", "fox" ); // Displays the SortedList. Console.WriteLine( "The SortedList contains the following:" ); PrintKeysAndValues( mySL ); } public static void PrintKeysAndValues( SortedList myList ) { Console.WriteLine( "\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList.Count; i++ ) { Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) ); } Console.WriteLine(); } } /* This code produces the following output. The SortedList contains the following: -KEY- -VALUE- four: fox one: The three: brown two: quick */
public virtual void Clear(); |
Exception Type | Condition |
---|---|
NotSupportedException | The SortedList is read-only. -or- The SortedList has a fixed size. |
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( "one", "The" ); mySL.Add( "two", "quick" ); mySL.Add( "three", "brown" ); mySL.Add( "four", "fox" ); mySL.Add( "five", "jumped" ); // Displays the count, capacity and values of the SortedList. Console.WriteLine( "Initially," ); Console.WriteLine( " Count : {0}", mySL.Count ); Console.WriteLine( " Capacity : {0}", mySL.Capacity ); Console.WriteLine( " Values:" ); PrintKeysAndValues( mySL ); // Trims the SortedList. mySL.TrimToSize(); // Displays the count, capacity and values of the SortedList. Console.WriteLine( "After TrimToSize," ); Console.WriteLine( " Count : {0}", mySL.Count ); Console.WriteLine( " Capacity : {0}", mySL.Capacity ); Console.WriteLine( " Values:" ); PrintKeysAndValues( mySL ); // Clears the SortedList. mySL.Clear(); // Displays the count, capacity and values of the SortedList. Console.WriteLine( "After Clear," ); Console.WriteLine( " Count : {0}", mySL.Count ); Console.WriteLine( " Capacity : {0}", mySL.Capacity ); Console.WriteLine( " Values:" ); PrintKeysAndValues( mySL ); // Trims the SortedList again. mySL.TrimToSize(); // Displays the count, capacity and values of the SortedList. Console.WriteLine( "After the second TrimToSize," ); Console.WriteLine( " Count : {0}", mySL.Count ); Console.WriteLine( " Capacity : {0}", mySL.Capacity ); Console.WriteLine( " Values:" ); PrintKeysAndValues( mySL ); } public static void PrintKeysAndValues( SortedList myList ) { Console.WriteLine( "\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList.Count; i++ ) { Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) ); } Console.WriteLine(); } } /* This code produces the following output. Initially, Count : 5 Capacity : 16 Values: -KEY- -VALUE- five: jumped four: fox one: The three: brown two: quick After TrimToSize, Count : 5 Capacity : 5 Values: -KEY- -VALUE- five: jumped four: fox one: The three: brown two: quick After Clear, Count : 0 Capacity : 16 Values: -KEY- -VALUE- After the second TrimToSize, Count : 0 Capacity : 16 Values: -KEY- -VALUE- */
public virtual object Clone(); |
In contrast, a deep copy of a collection copies the elements and everything directly or indirectly referenced by the elements.
key
Exception Type | Condition |
---|---|
ArgumentNullException | key is null. |
InvalidOperationException | The comparer throws an exception. |
This method uses a binary search algorithm; therefore, the average execution time is proportional to Log2 (n), where n is SortedList.Count.
SortedList.Contains implements IDictionary.Contains. It behaves exactly as SortedList.ContainsKey.
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( 0, "zero" ); mySL.Add( 1, "one" ); mySL.Add( 2, "two" ); mySL.Add( 3, "three" ); mySL.Add( 4, "four" ); // Displays the values of the SortedList. Console.WriteLine( "The SortedList contains the following values:" ); PrintIndexAndKeysAndValues( mySL ); // Searches for a specific key. int myKey = 2; Console.WriteLine( "The key \"{0}\" is {1}.", myKey, mySL.ContainsKey( myKey ) ? "in the SortedList" : "NOT in the SortedList" ); myKey = 6; Console.WriteLine( "The key \"{0}\" is {1}.", myKey, mySL.ContainsKey( myKey ) ? "in the SortedList" : "NOT in the SortedList" ); // Searches for a specific value. String myValue = "three"; Console.WriteLine( "The value \"{0}\" is {1}.", myValue, mySL.ContainsValue( myValue ) ? "in the SortedList" : "NOT in the SortedList" ); myValue = "nine"; Console.WriteLine( "The value \"{0}\" is {1}.", myValue, mySL.ContainsValue( myValue ) ? "in the SortedList" : "NOT in the SortedList" ); } public static void PrintIndexAndKeysAndValues( SortedList myList ) { Console.WriteLine( "\t-INDEX-\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList.Count; i++ ) { Console.WriteLine( "\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i) ); } Console.WriteLine(); } } /* This code produces the following output. The SortedList contains the following values: -INDEX- -KEY- -VALUE- [0]: 0 zero [1]: 1 one [2]: 2 two [3]: 3 three [4]: 4 four The key "2" is in the SortedList. The key "6" is NOT in the SortedList. The value "three" is in the SortedList. The value "nine" is NOT in the SortedList. */
key
Exception Type | Condition |
---|---|
ArgumentNullException | key is null. |
InvalidOperationException | The comparer throws an exception. |
This method uses a binary search algorithm; therefore, the average execution time is proportional to Log2 (n), where n is SortedList.Count.
This method behaves exactly as SortedList.Contains.
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( 0, "zero" ); mySL.Add( 1, "one" ); mySL.Add( 2, "two" ); mySL.Add( 3, "three" ); mySL.Add( 4, "four" ); // Displays the values of the SortedList. Console.WriteLine( "The SortedList contains the following values:" ); PrintIndexAndKeysAndValues( mySL ); // Searches for a specific key. int myKey = 2; Console.WriteLine( "The key \"{0}\" is {1}.", myKey, mySL.ContainsKey( myKey ) ? "in the SortedList" : "NOT in the SortedList" ); myKey = 6; Console.WriteLine( "The key \"{0}\" is {1}.", myKey, mySL.ContainsKey( myKey ) ? "in the SortedList" : "NOT in the SortedList" ); // Searches for a specific value. String myValue = "three"; Console.WriteLine( "The value \"{0}\" is {1}.", myValue, mySL.ContainsValue( myValue ) ? "in the SortedList" : "NOT in the SortedList" ); myValue = "nine"; Console.WriteLine( "The value \"{0}\" is {1}.", myValue, mySL.ContainsValue( myValue ) ? "in the SortedList" : "NOT in the SortedList" ); } public static void PrintIndexAndKeysAndValues( SortedList myList ) { Console.WriteLine( "\t-INDEX-\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList.Count; i++ ) { Console.WriteLine( "\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i) ); } Console.WriteLine(); } } /* This code produces the following output. The SortedList contains the following values: -INDEX- -KEY- -VALUE- [0]: 0 zero [1]: 1 one [2]: 2 two [3]: 3 three [4]: 4 four The key "2" is in the SortedList. The key "6" is NOT in the SortedList. The value "three" is in the SortedList. The value "nine" is NOT in the SortedList. */
value
The values of the elements of the SortedList are compared to the specified value using the Object.Equals method.
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( 0, "zero" ); mySL.Add( 1, "one" ); mySL.Add( 2, "two" ); mySL.Add( 3, "three" ); mySL.Add( 4, "four" ); // Displays the values of the SortedList. Console.WriteLine( "The SortedList contains the following values:" ); PrintIndexAndKeysAndValues( mySL ); // Searches for a specific key. int myKey = 2; Console.WriteLine( "The key \"{0}\" is {1}.", myKey, mySL.ContainsKey( myKey ) ? "in the SortedList" : "NOT in the SortedList" ); myKey = 6; Console.WriteLine( "The key \"{0}\" is {1}.", myKey, mySL.ContainsKey( myKey ) ? "in the SortedList" : "NOT in the SortedList" ); // Searches for a specific value. String myValue = "three"; Console.WriteLine( "The value \"{0}\" is {1}.", myValue, mySL.ContainsValue( myValue ) ? "in the SortedList" : "NOT in the SortedList" ); myValue = "nine"; Console.WriteLine( "The value \"{0}\" is {1}.", myValue, mySL.ContainsValue( myValue ) ? "in the SortedList" : "NOT in the SortedList" ); } public static void PrintIndexAndKeysAndValues( SortedList myList ) { Console.WriteLine( "\t-INDEX-\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList.Count; i++ ) { Console.WriteLine( "\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i) ); } Console.WriteLine(); } } /* This code produces the following output. The SortedList contains the following values: -INDEX- -KEY- -VALUE- [0]: 0 zero [1]: 1 one [2]: 2 two [3]: 3 three [4]: 4 four The key "2" is in the SortedList. The key "6" is NOT in the SortedList. The value "three" is in the SortedList. The value "nine" is NOT in the SortedList. */
array
arrayIndex
Exception Type | Condition |
---|---|
ArgumentNullException | array is null. |
ArgumentOutOfRangeException | arrayIndex is less than zero. |
ArgumentException | array is multidimensional. -or- arrayIndex is equal to or greater than the length of array. -or- The number of elements in the source SortedList is greater than the available space from arrayIndex to the end of the destination array. |
InvalidCastException | The type of the source SortedList cannot be cast automatically to the type of the destination array. |
To copy only the keys in the SortedList, use
SortedList.Keys.CopyTo
.
To copy only the values in the SortedList, use
SortedList.Values.CopyTo
.
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes the source SortedList. SortedList mySourceList = new SortedList(); mySourceList.Add( 0, "three" ); mySourceList.Add( 1, "napping" ); mySourceList.Add( 2, "cats" ); mySourceList.Add( 3, "in" ); mySourceList.Add( 4, "the" ); mySourceList.Add( 5, "barn" ); // Creates and initializes the one-dimensional target Array. Array myTargetArray=Array.CreateInstance( typeof(String), 15 ); myTargetArray.SetValue( "The", 0 ); myTargetArray.SetValue( "quick", 1 ); myTargetArray.SetValue( "brown", 2 ); myTargetArray.SetValue( "fox", 3 ); myTargetArray.SetValue( "jumped", 4 ); myTargetArray.SetValue( "over", 5 ); myTargetArray.SetValue( "the", 6 ); myTargetArray.SetValue( "lazy", 7 ); myTargetArray.SetValue( "dog", 8 ); // Displays the values of the target Array. Console.WriteLine( "The target Array contains the following (before and after copying):" ); PrintValues( myTargetArray, ' ' ); // Copies the entire source SortedList to the target SortedList, starting at index 6. mySourceList.CopyTo( myTargetArray, 6 ); // Displays the values of the target Array. PrintValues( myTargetArray, ' ' ); } public static void PrintValues( Array myArr, char mySeparator ) { System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator(); int i = 0; int cols = myArr.GetLength( myArr.Rank - 1 ); while ( myEnumerator.MoveNext() ) { if ( i < cols ) { i++; } else { Console.WriteLine(); i = 1; } Console.Write( "{0}{1}", mySeparator, myEnumerator.Current ); } Console.WriteLine(); } } /* This code produces the following output. The target Array contains the following (before and after copying): The quick brown fox jumped over the lazy dog The quick brown fox jumped over three napping cats in the barn */
~SortedList(); |
index
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | index is outside the range of valid indexes for the SortedList. |
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( 1.0, "The" ); mySL.Add( 1.1, "quick" ); mySL.Add( 1.2, "brown" ); mySL.Add( 1.3, "fox" ); mySL.Add( 1.4, "jumped" ); mySL.Add( 1.5, "over" ); mySL.Add( 1.6, "the" ); mySL.Add( 1.7, "lazy" ); mySL.Add( 1.8, "dog" ); // Gets the key and the value based on the index. int myIndex=3; Console.WriteLine( "The key at index {0} is {1}.", myIndex, mySL.GetKey( myIndex ) ); Console.WriteLine( "The value at index {0} is {1}.", myIndex, mySL.GetByIndex( myIndex ) ); // Gets the list of keys and the list of values. IList myKeyList = mySL.GetKeyList(); IList myValueList = mySL.GetValueList(); // Prints the keys in the first column and the values in the second column. Console.WriteLine( "\t-KEY-\t-VALUE-" ); for ( int i = 0; i < mySL.Count; i++ ) Console.WriteLine( "\t{0}\t{1}", myKeyList[i], myValueList[i] ); } } /* This code produces the following output. The key at index 3 is 1.3. The value at index 3 is fox. -KEY- -VALUE- 1 The 1.1 quick 1.2 brown 1.3 fox 1.4 jumped 1.5 over 1.6 the 1.7 lazy 1.8 dog */
public virtual IDictionaryEnumerator GetEnumerator(); |
Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. At this position, calling IEnumerator.Current throws an exception. Therefore, you must call IEnumerator.MoveNext to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.
IEnumerator.Current returns the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called. IEnumerator.MoveNext sets IEnumerator.Current to the next element.
After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling IEnumerator.MoveNext returns false. If the last call to IEnumerator.MoveNext returned false, calling IEnumerator.Current throws an exception. To set IEnumerator.Current to the first element of the collection again, you can call IEnumerator.Reset followed by IEnumerator.MoveNext.
An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying or deleting elements, the enumerator is irrecoverably invalidated and the next call to IEnumerator.MoveNext or IEnumerator.Reset throws an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current will return the element that it is set to, even if the enumerator is already invalidated.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
public virtual int GetHashCode(); |
index
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | index is outside the range of valid indexes for the SortedList. |
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( 1.0, "The" ); mySL.Add( 1.1, "quick" ); mySL.Add( 1.2, "brown" ); mySL.Add( 1.3, "fox" ); mySL.Add( 1.4, "jumped" ); mySL.Add( 1.5, "over" ); mySL.Add( 1.6, "the" ); mySL.Add( 1.7, "lazy" ); mySL.Add( 1.8, "dog" ); // Gets the key and the value based on the index. int myIndex=3; Console.WriteLine( "The key at index {0} is {1}.", myIndex, mySL.GetKey( myIndex ) ); Console.WriteLine( "The value at index {0} is {1}.", myIndex, mySL.GetByIndex( myIndex ) ); // Gets the list of keys and the list of values. IList myKeyList = mySL.GetKeyList(); IList myValueList = mySL.GetValueList(); // Prints the keys in the first column and the values in the second column. Console.WriteLine( "\t-KEY-\t-VALUE-" ); for ( int i = 0; i < mySL.Count; i++ ) Console.WriteLine( "\t{0}\t{1}", myKeyList[i], myValueList[i] ); } } /* This code produces the following output. The key at index 3 is 1.3. The value at index 3 is fox. -KEY- -VALUE- 1 The 1.1 quick 1.2 brown 1.3 fox 1.4 jumped 1.5 over 1.6 the 1.7 lazy 1.8 dog */
public virtual IList GetKeyList(); |
The elements of the returned IList are sorted in the same order as the keys of the SortedList.
Similar to SortedList.Keys, but returns an IList instead of an ICollection.
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( 1.0, "The" ); mySL.Add( 1.1, "quick" ); mySL.Add( 1.2, "brown" ); mySL.Add( 1.3, "fox" ); mySL.Add( 1.4, "jumped" ); mySL.Add( 1.5, "over" ); mySL.Add( 1.6, "the" ); mySL.Add( 1.7, "lazy" ); mySL.Add( 1.8, "dog" ); // Gets the key and the value based on the index. int myIndex=3; Console.WriteLine( "The key at index {0} is {1}.", myIndex, mySL.GetKey( myIndex ) ); Console.WriteLine( "The value at index {0} is {1}.", myIndex, mySL.GetByIndex( myIndex ) ); // Gets the list of keys and the list of values. IList myKeyList = mySL.GetKeyList(); IList myValueList = mySL.GetValueList(); // Prints the keys in the first column and the values in the second column. Console.WriteLine( "\t-KEY-\t-VALUE-" ); for ( int i = 0; i < mySL.Count; i++ ) Console.WriteLine( "\t{0}\t{1}", myKeyList[i], myValueList[i] ); } } /* This code produces the following output. The key at index 3 is 1.3. The value at index 3 is fox. -KEY- -VALUE- 1 The 1.1 quick 1.2 brown 1.3 fox 1.4 jumped 1.5 over 1.6 the 1.7 lazy 1.8 dog */
public Type GetType(); |
public virtual IList GetValueList(); |
The elements of the returned IList are sorted in the same order as the values of the SortedList.
Similar to SortedList.Values, but returns an IList instead of an ICollection.
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( 1.0, "The" ); mySL.Add( 1.1, "quick" ); mySL.Add( 1.2, "brown" ); mySL.Add( 1.3, "fox" ); mySL.Add( 1.4, "jumped" ); mySL.Add( 1.5, "over" ); mySL.Add( 1.6, "the" ); mySL.Add( 1.7, "lazy" ); mySL.Add( 1.8, "dog" ); // Gets the key and the value based on the index. int myIndex=3; Console.WriteLine( "The key at index {0} is {1}.", myIndex, mySL.GetKey( myIndex ) ); Console.WriteLine( "The value at index {0} is {1}.", myIndex, mySL.GetByIndex( myIndex ) ); // Gets the list of keys and the list of values. IList myKeyList = mySL.GetKeyList(); IList myValueList = mySL.GetValueList(); // Prints the keys in the first column and the values in the second column. Console.WriteLine( "\t-KEY-\t-VALUE-" ); for ( int i = 0; i < mySL.Count; i++ ) Console.WriteLine( "\t{0}\t{1}", myKeyList[i], myValueList[i] ); } } /* This code produces the following output. The key at index 3 is 1.3. The value at index 3 is fox. -KEY- -VALUE- 1 The 1.1 quick 1.2 brown 1.3 fox 1.4 jumped 1.5 over 1.6 the 1.7 lazy 1.8 dog */
key
Exception Type | Condition |
---|---|
ArgumentNullException | key is null. |
InvalidOperationException | The comparer throws an exception. |
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.
This method uses a binary search algorithm; therefore, the average execution time is proportional to Log2 (n), where n is SortedList.Count.
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( 0, "zero" ); mySL.Add( 1, "one" ); mySL.Add( 2, "two" ); mySL.Add( 3, "three" ); mySL.Add( 4, "four" ); // Displays the values of the SortedList. Console.WriteLine( "The SortedList contains the following values:" ); PrintIndexAndKeysAndValues( mySL ); // Searches for a specific key. int myKey = 2; Console.WriteLine( "The key \"{0}\" is at index {1}.", myKey, mySL.IndexOfKey( myKey ) ); // Searches for a specific value. String myValue = "three"; Console.WriteLine( "The value \"{0}\" is at index {1}.", myValue, mySL.IndexOfValue( myValue ) ); } public static void PrintIndexAndKeysAndValues( SortedList myList ) { Console.WriteLine( "\t-INDEX-\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList.Count; i++ ) { Console.WriteLine( "\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i) ); } Console.WriteLine(); } } /* This code produces the following output. The SortedList contains the following values: -INDEX- -KEY- -VALUE- [0]: 0 zero [1]: 1 one [2]: 2 two [3]: 3 three [4]: 4 four The key "2" is at index 2. The value "three" is at index 3. */
value
The values of the elements of the SortedList are compared to the specified value using the Object.Equals method.
This method uses a linear search; therefore, the average execution time is proportional to SortedList.Count.
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( 0, "zero" ); mySL.Add( 1, "one" ); mySL.Add( 2, "two" ); mySL.Add( 3, "three" ); mySL.Add( 4, "four" ); // Displays the values of the SortedList. Console.WriteLine( "The SortedList contains the following values:" ); PrintIndexAndKeysAndValues( mySL ); // Searches for a specific key. int myKey = 2; Console.WriteLine( "The key \"{0}\" is at index {1}.", myKey, mySL.IndexOfKey( myKey ) ); // Searches for a specific value. String myValue = "three"; Console.WriteLine( "The value \"{0}\" is at index {1}.", myValue, mySL.IndexOfValue( myValue ) ); } public static void PrintIndexAndKeysAndValues( SortedList myList ) { Console.WriteLine( "\t-INDEX-\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList.Count; i++ ) { Console.WriteLine( "\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i) ); } Console.WriteLine(); } } /* This code produces the following output. The SortedList contains the following values: -INDEX- -KEY- -VALUE- [0]: 0 zero [1]: 1 one [2]: 2 two [3]: 3 three [4]: 4 four The key "2" is at index 2. The value "three" is at index 3. */
protected object MemberwiseClone(); |
public virtual void Remove( |
key
Exception Type | Condition |
---|---|
ArgumentNullException | key is null. |
NotSupportedException | The SortedList is read-only. -or- The SortedList has a fixed size. |
In collections of contiguous elements, such as lists, the elements that follow the removed element move up to occupy the vacated spot. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( "1a", "The" ); mySL.Add( "1b", "quick" ); mySL.Add( "1c", "brown" ); mySL.Add( "2a", "fox" ); mySL.Add( "2b", "jumped" ); mySL.Add( "2c", "over" ); mySL.Add( "3a", "the" ); mySL.Add( "3b", "lazy" ); mySL.Add( "3c", "dog" ); // Displays the SortedList. Console.WriteLine( "The SortedList initially contains the following:" ); PrintKeysAndValues( mySL ); // Removes the element with the key "3b". mySL.Remove( "3b" ); // Displays the current state of the SortedList. Console.WriteLine( "After removing \"lazy\":" ); PrintKeysAndValues( mySL ); // Removes the element at index 5. mySL.RemoveAt( 5 ); // Displays the current state of the SortedList. Console.WriteLine( "After removing the element at index 5:" ); PrintKeysAndValues( mySL ); } public static void PrintKeysAndValues( SortedList myList ) { Console.WriteLine( "\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList.Count; i++ ) { Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) ); } Console.WriteLine(); } } /* This code produces the following output. The SortedList initially contains the following: -KEY- -VALUE- 1a: The 1b: quick 1c: brown 2a: fox 2b: jumped 2c: over 3a: the 3b: lazy 3c: dog After removing "lazy": -KEY- -VALUE- 1a: The 1b: quick 1c: brown 2a: fox 2b: jumped 2c: over 3a: the 3c: dog After removing the element at index 5: -KEY- -VALUE- 1a: The 1b: quick 1c: brown 2a: fox 2b: jumped 3a: the 3c: dog */
public virtual void RemoveAt( |
index
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | index is outside the range of valid indexes for the SortedList. |
NotSupportedException | The SortedList is read-only. -or- The SortedList has a fixed size. |
In collections of contiguous elements, such as lists, the elements that follow the removed element move up to occupy the vacated spot. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( "1a", "The" ); mySL.Add( "1b", "quick" ); mySL.Add( "1c", "brown" ); mySL.Add( "2a", "fox" ); mySL.Add( "2b", "jumped" ); mySL.Add( "2c", "over" ); mySL.Add( "3a", "the" ); mySL.Add( "3b", "lazy" ); mySL.Add( "3c", "dog" ); // Displays the SortedList. Console.WriteLine( "The SortedList initially contains the following:" ); PrintKeysAndValues( mySL ); // Removes the element with the key "3b". mySL.Remove( "3b" ); // Displays the current state of the SortedList. Console.WriteLine( "After removing \"lazy\":" ); PrintKeysAndValues( mySL ); // Removes the element at index 5. mySL.RemoveAt( 5 ); // Displays the current state of the SortedList. Console.WriteLine( "After removing the element at index 5:" ); PrintKeysAndValues( mySL ); } public static void PrintKeysAndValues( SortedList myList ) { Console.WriteLine( "\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList.Count; i++ ) { Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) ); } Console.WriteLine(); } } /* This code produces the following output. The SortedList initially contains the following: -KEY- -VALUE- 1a: The 1b: quick 1c: brown 2a: fox 2b: jumped 2c: over 3a: the 3b: lazy 3c: dog After removing "lazy": -KEY- -VALUE- 1a: The 1b: quick 1c: brown 2a: fox 2b: jumped 2c: over 3a: the 3c: dog After removing the element at index 5: -KEY- -VALUE- 1a: The 1b: quick 1c: brown 2a: fox 2b: jumped 3a: the 3c: dog */
index
value
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | index is outside the range of valid indexes for the SortedList. |
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( 0, "zero" ); mySL.Add( 1, "one" ); mySL.Add( 2, "two" ); mySL.Add( 3, "three" ); mySL.Add( 4, "four" ); // Displays the values of the SortedList. Console.WriteLine( "The SortedList contains the following values:" ); PrintIndexAndKeysAndValues( mySL ); // Replaces the values at index 3 and index 4. mySL.SetByIndex( 3, "III" ); mySL.SetByIndex( 4, "IV" ); // Displays the updated values of the SortedList. Console.WriteLine( "After replacing the value at index 3 and index 4," ); PrintIndexAndKeysAndValues( mySL ); } public static void PrintIndexAndKeysAndValues( SortedList myList ) { Console.WriteLine( "\t-INDEX-\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList.Count; i++ ) { Console.WriteLine( "\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i) ); } Console.WriteLine(); } } /* This code produces the following output. The SortedList contains the following values: -INDEX- -KEY- -VALUE- [0]: 0 zero [1]: 1 one [2]: 2 two [3]: 3 three [4]: 4 four After replacing the value at index 3 and index 4, -INDEX- -KEY- -VALUE- [0]: 0 zero [1]: 1 one [2]: 2 two [3]: 3 III [4]: 4 IV */
public static SortedList Synchronized( |
list
Exception Type | Condition |
---|---|
ArgumentNullException | list is null. |
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
The following code example shows how to lock the collection using the SortedList.SyncRoot during the entire enumeration:
SortedList myCollection = new SortedList(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( 0, "zero" ); mySL.Add( 1, "one" ); mySL.Add( 2, "two" ); mySL.Add( 3, "three" ); mySL.Add( 4, "four" ); // Creates a synchronized wrapper around the SortedList. SortedList mySyncdSL = SortedList.Synchronized( mySL ); // Displays the sychronization status of both SortedLists. Console.WriteLine( "mySL is {0}.", mySL.IsSynchronized ? "synchronized" : "not synchronized" ); Console.WriteLine( "mySyncdSL is {0}.", mySyncdSL.IsSynchronized ? "synchronized" : "not synchronized" ); } } /* This code produces the following output. mySL is not synchronized. mySyncdSL is synchronized. */
IEnumerator IEnumerable.GetEnumerator(); |
Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. At this position, calling IEnumerator.Current throws an exception. Therefore, you must call IEnumerator.MoveNext to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.
IEnumerator.Current returns the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called. IEnumerator.MoveNext sets IEnumerator.Current to the next element.
After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling IEnumerator.MoveNext returns false. If the last call to IEnumerator.MoveNext returned false, calling IEnumerator.Current throws an exception. To set IEnumerator.Current to the first element of the collection again, you can call IEnumerator.Reset followed by IEnumerator.MoveNext.
An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying or deleting elements, the enumerator is irrecoverably invalidated and the next call to IEnumerator.MoveNext or IEnumerator.Reset throws an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current will return the element that it is set to, even if the enumerator is already invalidated.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
public virtual string ToString(); |
public virtual void TrimToSize(); |
Exception Type | Condition |
---|---|
NotSupportedException | The SortedList is read-only. -or- The SortedList has a fixed size. |
To completely clear all elements in a list, call the SortedList.Clear method before calling SortedList.TrimToSize. Trimming an empty SortedList sets the capacity of the SortedList to the default capacity, not zero.
using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( "one", "The" ); mySL.Add( "two", "quick" ); mySL.Add( "three", "brown" ); mySL.Add( "four", "fox" ); mySL.Add( "five", "jumped" ); // Displays the count, capacity and values of the SortedList. Console.WriteLine( "Initially," ); Console.WriteLine( " Count : {0}", mySL.Count ); Console.WriteLine( " Capacity : {0}", mySL.Capacity ); Console.WriteLine( " Values:" ); PrintKeysAndValues( mySL ); // Trims the SortedList. mySL.TrimToSize(); // Displays the count, capacity and values of the SortedList. Console.WriteLine( "After TrimToSize," ); Console.WriteLine( " Count : {0}", mySL.Count ); Console.WriteLine( " Capacity : {0}", mySL.Capacity ); Console.WriteLine( " Values:" ); PrintKeysAndValues( mySL ); // Clears the SortedList. mySL.Clear(); // Displays the count, capacity and values of the SortedList. Console.WriteLine( "After Clear," ); Console.WriteLine( " Count : {0}", mySL.Count ); Console.WriteLine( " Capacity : {0}", mySL.Capacity ); Console.WriteLine( " Values:" ); PrintKeysAndValues( mySL ); // Trims the SortedList again. mySL.TrimToSize(); // Displays the count, capacity and values of the SortedList. Console.WriteLine( "After the second TrimToSize," ); Console.WriteLine( " Count : {0}", mySL.Count ); Console.WriteLine( " Capacity : {0}", mySL.Capacity ); Console.WriteLine( " Values:" ); PrintKeysAndValues( mySL ); } public static void PrintKeysAndValues( SortedList myList ) { Console.WriteLine( "\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList.Count; i++ ) { Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) ); } Console.WriteLine(); } } /* This code produces the following output. Initially, Count : 5 Capacity : 16 Values: -KEY- -VALUE- five: jumped four: fox one: The three: brown two: quick After TrimToSize, Count : 5 Capacity : 5 Values: -KEY- -VALUE- five: jumped four: fox one: The three: brown two: quick After Clear, Count : 0 Capacity : 16 Values: -KEY- -VALUE- After the second TrimToSize, Count : 0 Capacity : 16 Values: -KEY- -VALUE- */