System.Collections.SortedList Class

Assembly: Mscorlib.dll
Namespace: System.Collections
Summary
Represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index.
C# Syntax:
[Serializable]
public class SortedList : IDictionary, ICollection, IEnumerable, ICloneable
Thread Safety
Public static (non-instance) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.

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.

Remarks
A SortedList is a hybrid between a Hashtable and an Array. When an element is accessed by its key using the SortedList.Item indexer property, it behaves like a Hashtable. When an element is accessed by its index using SortedList.GetByIndex or SortedList.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 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) {...}

Example
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 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:    !
 */ 

    
See also:
System.Collections Namespace | IComparable | IComparer | IDictionary | Hashtable

System.Collections.SortedList Member List:

Public Constructors
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.
Public Properties
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.
Public Methods
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.
Protected Methods
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.
Explicit Interface Implementations 
IEnumerable.GetEnumerator Returns an IEnumerator that can iterate through the SortedList.

Hierarchy:


System.Collections.SortedList Member Details

Overloaded ctor #1
Summary
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.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public SortedList();
Remarks
The initial capacity is the starting capacity of the new SortedList. The default initial capacity for a SortedList is 16.

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.

See also:
IComparable | SortedList.Capacity

Return to top


Overloaded ctor #2
Summary
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.
C# Syntax:
public SortedList(
   IComparer comparer
);
Parameters:

comparer

The IComparer implementation to use when comparing keys.

-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.

Remarks
The initial capacity is the starting capacity of the new SortedList. The default initial capacity for a SortedList is 16.

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.

See also:
IComparer | IComparable | SortedList.Capacity

Return to top


Overloaded ctor #3
Summary
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.
C# Syntax:
public SortedList(
   IDictionary d
);
Parameters:

d

The IDictionary to copy to a new SortedList.

Exceptions
Exception Type Condition
ArgumentNullException d is null.
InvalidCastException One or more elements in d do not implement the IComparable interface.
Remarks
The initial capacity is the starting capacity of the new SortedList. When adding elements to the list, if the number of elements exceeds 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.

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.

See also:
IDictionary | IComparable | Hashtable | SortedList.Capacity

Return to top


Overloaded ctor #4
Summary
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.
C# Syntax:
public SortedList(
   int initialCapacity
);
Parameters:

initialCapacity

The initial number of elements that the SortedList can contain.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException initialCapacity is less than zero.
Remarks
The initial capacity is the starting capacity of the new SortedList. The default initial capacity for a SortedList is 16.

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.

See also:
IComparable | SortedList.Capacity

Return to top


Overloaded ctor #5
Summary
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.
C# Syntax:
public SortedList(
   IComparer comparer,
   int capacity
);
Parameters:

comparer

The IComparer implementation to use when comparing keys.

-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

The initial number of elements that the SortedList can contain.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException initialCapacity is less than zero.
Remarks
The initial capacity is the starting capacity of the new SortedList. The default initial capacity for a SortedList is 16.

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.

See also:
IComparer | IComparable | SortedList.Capacity

Return to top


Overloaded ctor #6
Summary
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.
C# Syntax:
public SortedList(
   IDictionary d,
   IComparer comparer
);
Parameters:

d

The IDictionary to copy to a new SortedList.

comparer

The IComparer implementation to use when comparing keys.

-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.

Exceptions
Exception Type Condition
ArgumentNullException d is null.
InvalidCastException comparer is null, and one or more elements in d do not implement the IComparable interface.
Remarks
The initial capacity is the starting capacity of the new SortedList. When adding elements to the list, if the number of elements exceeds 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.

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.

See also:
IDictionary | IComparer | IComparable | Hashtable | SortedList.Capacity

Return to top


Property: Capacity (read-write)
Summary
Gets or sets the capacity of the SortedList.
C# Syntax:
public virtual int Capacity {get; set;}
Exceptions
Exception Type Condition
ArgumentOutOfRangeException The value assigned is less than the current number of elements in the SortedList.
Remarks
If the number of elements added to the list reaches the current capacity, the capacity is automatically doubled.

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.

See also:
SortedList.TrimToSize

Return to top


Property: Count (read-only)
Summary
Gets the number of elements contained in the SortedList.
C# Syntax:
public virtual int Count {get;}
Implements:
ICollection.Count
Remarks
Each element is a key-and-value pair that can be accessed as a DictionaryEntry object.
See also:
SortedList.Capacity

Return to top


Property: IsFixedSize (read-only)
Summary
Gets a value indicating whether the SortedList has a fixed size.
C# Syntax:
public virtual bool IsFixedSize {get;}
Implements:
IDictionary.IsFixedSize
Remarks
A collection with a fixed size does not allow the addition or removal of elements after the collection is created, but it allows the modification of existing elements.

Return to top


Property: IsReadOnly (read-only)
Summary
Gets a value indicating whether the SortedList is read-only.
C# Syntax:
public virtual bool IsReadOnly {get;}
Implements:
IDictionary.IsReadOnly
Remarks
A collection that is read-only does not allow the addition, removal, or modification of elements after the collection is created.

Return to top


Property: IsSynchronized (read-only)
Summary
Gets a value indicating whether access to the SortedList is synchronized (thread-safe).
C# Syntax:
public virtual bool IsSynchronized {get;}
Implements:
ICollection.IsSynchronized
Remarks
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.

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.
               }
              }
            
Example
The following example shows how to synchronize a SortedList, determine if a SortedList is synchronized and use a synchronized 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" );
 
       // 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.
 */ 

    
See also:
SortedList.SyncRoot | SortedList.Synchronized

Return to top


Property: Item (read-write)
Summary
Gets and sets the value associated with a specific key in the SortedList.
C# Syntax:
public virtual object this[object key] {get; set;}
Parameters:

key

The key associated with the value to get or set.

Exceptions
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.
Implements:
IDictionary.Item
Remarks
If setting the value of key and key does not exist in the SortedList, a new element is created with the specified key and the specified value.

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.

See also:
SortedList.Add | SortedList.Contains | SortedList.ContainsKey

Return to top


Property: Keys (read-only)
Summary
Gets the keys in the SortedList.
C# Syntax:
public virtual ICollection Keys {get;}
Implements:
IDictionary.Keys
Remarks
The ICollection is a read-only view of the keys of the SortedList. Modifications made to the underlying SortedList are immediately reflected in the ICollection.

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.

See also:
ICollection | SortedList.Values | SortedList.GetKeyList

Return to top


Property: SyncRoot (read-only)
Summary
Gets an object that can be used to synchronize access to the SortedList.
C# Syntax:
public virtual object SyncRoot {get;}
Implements:
ICollection.SyncRoot
Remarks
To create a synchronized version of the SortedList, use the SortedList.Synchronized method. However, derived classes can provide their own synchronized version of the SortedList using the SortedList.SyncRoot property. The synchronizing code must perform operations on the SortedList.SyncRoot of the SortedList, not directly on the SortedList. This ensures proper operation of collections that are derived from other objects. Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the SortedList object.

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.
               }
              }
            
See also:
SortedList.IsSynchronized | SortedList.Synchronized

Return to top


Property: Values (read-only)
Summary
Gets the values in the SortedList.
C# Syntax:
public virtual ICollection Values {get;}
Implements:
IDictionary.Values
Remarks
The ICollection is a read-only view of the values of the SortedList. Modifications made to the underlying SortedList are immediately reflected in the ICollection.

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.

See also:
ICollection | SortedList.Keys | SortedList.GetValueList

Return to top


Method: Add(
   object key,
   object value
)
Summary
Adds an element with the specified key and value to the SortedList.
C# Syntax:
public virtual void Add(
   object key,
   object value
);
Parameters:

key

The key of the element to add.

value

The value of the element to add.

Exceptions
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.
Implements:
IDictionary.Add
Remarks
If the number of elements added to the list reaches the current capacity, the capacity is automatically doubled. The insertion point is determined based on the comparer selected, either explicitly or by default when the SortedList was created.

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.

Example
The following example shows how to add elements to 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( "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
 */

    
See also:
SortedList.Item | IComparer | IComparable | SortedList.Capacity

Return to top


Method: Clear()
Summary
Removes all elements from the SortedList.
C# Syntax:
public virtual void Clear();
Exceptions
Exception Type Condition
NotSupportedException The SortedList is read-only.

-or-

The SortedList has a fixed size.

Implements:
IDictionary.Clear
Remarks
SortedList.Count is set to zero. SortedList.Capacity remains unchanged. To reset the capacity of the SortedList, call SortedList.TrimToSize or set the SortedList.Capacity property directly. Trimming an empty SortedList sets the capacity of the SortedList to the default capacity, not zero.
Example
The following example shows how to trim the unused portions of the SortedList and how to clear the values of 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( "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-
 */ 

    
See also:
SortedList.TrimToSize | SortedList.Capacity | SortedList.Count

Return to top


Method: Clone()
Summary
Creates a shallow copy of the SortedList.
C# Syntax:
public virtual object Clone();
Return Value:
A shallow copy of the SortedList.
Implements:
ICloneable.Clone
Remarks
A shallow copy of a collection copies only the elements of the collection, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new collection point to the same objects that the references in the original collection point to.

In contrast, a deep copy of a collection copies the elements and everything directly or indirectly referenced by the elements.

See also:
SortedList.CopyTo

Return to top


Method: Contains(
   object key
)
Summary
Determines whether the SortedList contains a specific key.
C# Syntax:
public virtual bool Contains(
   object key
);
Parameters:

key

The key to locate in the SortedList.

Return Value:
true if the SortedList contains an element with the specified key; otherwise, false.
Exceptions
Exception Type Condition
ArgumentNullException key is null.
InvalidOperationException The comparer throws an exception.
Implements:
IDictionary.Contains
Remarks
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.

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.

Example
The following example shows how to determine whether the SortedList contains a specific element.
 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.
 */ 

    
See also:
SortedList.ContainsKey | SortedList.IndexOfKey

Return to top


Method: ContainsKey(
   object key
)
Summary
Determines whether the SortedList contains a specific key.
C# Syntax:
public virtual bool ContainsKey(
   object key
);
Parameters:

key

The key to locate in the SortedList.

Return Value:
true if the SortedList contains an element with the specified key; otherwise, false.
Exceptions
Exception Type Condition
ArgumentNullException key is null.
InvalidOperationException The comparer throws an exception.
Remarks
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.

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.

Example
The following example shows how to determine whether the SortedList contains a specific element.
 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.
 */ 

    
See also:
SortedList.Contains | SortedList.ContainsValue | SortedList.IndexOfKey

Return to top


Method: ContainsValue(
   object value
)
Summary
Determines whether the SortedList contains a specific value.
C# Syntax:
public virtual bool ContainsValue(
   object value
);
Parameters:

value

The value to locate in the SortedList.

Return Value:
true if the SortedList contains an element with the specified value; otherwise, false.
Remarks
This method performs a linear search; therefore, the average execution time is proportional to SortedList.Count. That is, this method is an O(n) operation, where n is SortedList.Count.

The values of the elements of the SortedList are compared to the specified value using the Object.Equals method.

Example
The following example shows how to determine whether the SortedList contains a specific element.
 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.
 */ 

    
See also:
SortedList.ContainsKey | SortedList.IndexOfValue

Return to top


Method: CopyTo(
   Array array,
   int arrayIndex
)
Summary
Copies the SortedList elements to a one-dimensional Array instance at the specified index.
C# Syntax:
public virtual void CopyTo(
   Array array,
   int arrayIndex
);
Parameters:

array

The one-dimensional Array that is the destination of the DictionaryEntry objects copied from SortedList. The Array must have zero-based indexing.

arrayIndex

The zero-based index in array at which copying begins.

Exceptions
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.
Implements:
ICollection.CopyTo
Remarks
The key-and-value pairs are copied to the Array in the same order in which the enumerator iterates through the SortedList.

To copy only the keys in the SortedList, use SortedList.Keys.CopyTo .

To copy only the values in the SortedList, use SortedList.Values.CopyTo .

Example
The following example shows how to copy the values in a SortedList into a one-dimensional Array.
 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
 */ 

    
See also:
Array | DictionaryEntry | SortedList.GetEnumerator

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~SortedList();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetByIndex(
   int index
)
Summary
Gets the value at the specified index of the SortedList.
C# Syntax:
public virtual object GetByIndex(
   int index
);
Parameters:

index

The zero-based index of the value to get.

Return Value:
The value at the specified index of the SortedList.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is outside the range of valid indexes for the SortedList.
Remarks
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.
Example
The following example shows how to get one or all the keys or values in 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
 */ 

    
See also:
SortedList.IndexOfKey | SortedList.IndexOfValue

Return to top


Method: GetEnumerator()
Summary
Returns an IDictionaryEnumerator that can iterate through the SortedList.
C# Syntax:
public virtual IDictionaryEnumerator GetEnumerator();
Return Value:
An IDictionaryEnumerator for the SortedList.
Implements:
IDictionary.GetEnumerator
Remarks
Enumerators only allow reading the data in the collection. Enumerators cannot be used to modify the underlying collection.

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.

See also:
IDictionaryEnumerator | IEnumerator

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetKey(
   int index
)
Summary
Gets the key at the specified index of the SortedList.
C# Syntax:
public virtual object GetKey(
   int index
);
Parameters:

index

The zero-based index of the key to get.

Return Value:
The key at the specified index of the SortedList.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is outside the range of valid indexes for the SortedList.
Remarks
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.
Example
The following example shows how to get one or all the keys or values in 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
 */ 

    

Return to top


Method: GetKeyList()
Summary
Gets the keys in the SortedList.
C# Syntax:
public virtual IList GetKeyList();
Return Value:
An IList containing the keys in the SortedList.
Remarks
The returned IList is a read-only view of the keys of the SortedList. Modifications made to the underlying SortedList are immediately reflected in the IList.

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.

Example
The following example shows how to get one or all the keys or values in 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
 */ 

    
See also:
IList | SortedList.GetValueList | SortedList.Keys

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetValueList()
Summary
Gets the values in the SortedList.
C# Syntax:
public virtual IList GetValueList();
Return Value:
An IList containing the values in the SortedList.
Remarks
The returned IList is a read-only view of the values of the SortedList. Modifications made to the underlying SortedList are immediately reflected in the IList.

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.

Example
The following example shows how to get one or all the keys or values in 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
 */ 

    
See also:
IList | SortedList.GetKeyList | SortedList.Values

Return to top


Method: IndexOfKey(
   object key
)
Summary
Returns the zero-based index of the specified key in the SortedList.
C# Syntax:
public virtual int IndexOfKey(
   object key
);
Parameters:

key

The key to locate in the SortedList.

Return Value:
The zero-based index of key, if key is found in the SortedList; otherwise, -1.
Exceptions
Exception Type Condition
ArgumentNullException key is null.
InvalidOperationException The comparer throws an exception.
Remarks
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.

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.

Example
The following example shows how to determine the index of a key or a value in a 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 );
 
       // 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.
 */ 

    
See also:
SortedList.ContainsKey | SortedList.IndexOfValue

Return to top


Method: IndexOfValue(
   object value
)
Summary
Returns the zero-based index of the first occurrence of the specified value in the SortedList.
C# Syntax:
public virtual int IndexOfValue(
   object value
);
Parameters:

value

The value to locate in the SortedList.

Return Value:
The zero-based index of the first occurrence of value, if value is found in the SortedList; otherwise, -1.
Remarks
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.

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.

Example
The following example shows how to determine the index of a key or a value in a 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 );
 
       // 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.
 */ 

    
See also:
SortedList.ContainsValue | SortedList.IndexOfKey

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Remove(
   object key
)
Summary
Removes the element with the specified key from SortedList.
C# Syntax:
public virtual void Remove(
   object key
);
Parameters:

key

The key of the element to remove.

Exceptions
Exception Type Condition
ArgumentNullException key is null.
NotSupportedException The SortedList is read-only.

-or-

The SortedList has a fixed size.

Implements:
IDictionary.Remove
Remarks
If the SortedList does not contain an element with the specified key, the SortedList remains unchanged. No exception is thrown.

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.

Example
The following example shows how to remove elements from 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( "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
 */

    
See also:
SortedList.RemoveAt

Return to top


Method: RemoveAt(
   int index
)
Summary
Removes the element at the specified index of SortedList.
C# Syntax:
public virtual void RemoveAt(
   int index
);
Parameters:

index

The zero-based index of the element to remove.

Exceptions
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.

Remarks
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.

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.

Example
The following example shows how to remove elements from 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( "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
 */

    
See also:
SortedList.Remove

Return to top


Method: SetByIndex(
   int index,
   object value
)
Summary
Replaces the value at a specific index in the SortedList.
C# Syntax:
public virtual void SetByIndex(
   int index,
   object value
);
Parameters:

index

The zero-based index at which to save value.

value

The Object to save into the SortedList.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is outside the range of valid indexes for the SortedList.
Remarks
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.
Example
The following example shows how to replace the value of an existing element in 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
 */ 

    
See also:
SortedList.IndexOfKey | SortedList.IndexOfValue

Return to top


Method: Synchronized(
   SortedList list
)
Summary
Returns a synchronized (thread-safe) wrapper for the SortedList.
C# Syntax:
public static SortedList Synchronized(
   SortedList list
);
Parameters:

list

The SortedList to synchronize.

Return Value:
A synchronized (thread-safe) wrapper for the SortedList.
Exceptions
Exception Type Condition
ArgumentNullException list is null.
Remarks
To guarantee the thread safety of the SortedList, all operations must be done through this wrapper only.

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.
               }
              }
            
Example
The following example shows how to synchronize a SortedList, determine if a SortedList is synchronized and use a synchronized 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" );
 
       // 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.
 */ 

    
See also:
SortedList.IsSynchronized | SortedList.SyncRoot

Return to top


Method: IEnumerable.GetEnumerator()
Summary
Returns an IEnumerator that can iterate through the SortedList.
C# Syntax:
IEnumerator IEnumerable.GetEnumerator();
Return Value:
An IEnumerator for the SortedList.
Implements:
IEnumerable.GetEnumerator
Remarks
Enumerators only allow reading the data in the collection. Enumerators cannot be used to modify the underlying collection.

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.

See also:
IDictionaryEnumerator | IEnumerator

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: TrimToSize()
Summary
Sets the capacity to the actual number of elements in the SortedList.
C# Syntax:
public virtual void TrimToSize();
Exceptions
Exception Type Condition
NotSupportedException The SortedList is read-only.

-or-

The SortedList has a fixed size.

Remarks
This method can be used to minimize a list's memory overhead if no new elements will be added to the list.

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.

Example
The following example shows how to trim the unused portions of the SortedList and how to clear the values of 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( "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-
 */ 

    
See also:
SortedList.Clear | SortedList.Capacity | SortedList.Count

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.