asp.net.ph

SortedList Class

System.Collections Namespace


Represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index.

SortedList Class Members

Collapse   Constructors

Visibility Constructor Parameters
public SortedList ( )
public SortedList ( Int32 initialCapacity )
public SortedList ( IComparer comparer )
public SortedList ( IComparer comparer , Int32 capacity )
public SortedList ( IDictionary d )
public SortedList ( IDictionary d , IComparer comparer )

Collapse   Properties

Visibility Name Value Type Accessibility
public Capacity Int32 [ Get , Set ]
public Count Int32 [ Get ]
public IsFixedSize Boolean [ Get ]
public IsReadOnly Boolean [ Get ]
public IsSynchronized Boolean [ Get ]
public Item ( Object key ) Object [ Get , Set ]
public Keys ICollection [ Get ]
public SyncRoot Object [ Get ]
public Values ICollection [ Get ]

Collapse   Methods

Visibility Name Parameters Return Type
public Add ( Object key , Object value ) Void
public Clear ( ) Void
public Clone ( ) Object
public Contains ( Object key ) Boolean
public ContainsKey ( Object key ) Boolean
public ContainsValue ( Object value ) Boolean
public CopyTo ( Array array , Int32 arrayIndex ) Void
public GetByIndex ( Int32 index ) Object
public GetEnumerator ( ) IDictionaryEnumerator
public GetKey ( Int32 index ) Object
public GetKeyList ( ) IList
public GetValueList ( ) IList
public IndexOfKey ( Object key ) Int32
public IndexOfValue ( Object value ) Int32
public Remove ( Object key ) Void
public RemoveAt ( Int32 index ) Void
public SetByIndex ( Int32 index , Object value ) Void
public static Synchronized ( SortedList list ) SortedList
public TrimToSize ( ) Void

Remarks

A SortedList is a hybrid between a Hashtable and an Array. When an element is accessed by its key using the Item indexer property, it behaves like a Hashtable. When an element is accessed by its index using GetByIndex or SetByIndex, it behaves like an Array.

A SortedList internally maintains two arrays to store the elements of the list; that is, one array for the keys and another array for the associated values. Each element is a key-and-value pair that can be accessed as a DictionaryEntry object.

The capacity of a SortedList is the number of elements that the list can hold. As elements are added to a SortedList, the capacity is automatically increased as required through reallocation. The capacity can be decreased by calling TrimToSize or by setting the Capacity property explicitly.

The elements of a SortedList are sorted by the keys either according to a specific IComparer implementation specified when the SortedList is created or according to the IComparable implementation provided by the keys themselves. In either case, a SortedList does not allow duplicate keys.

The index sequence is based on the sort sequence. When an element is added, it is inserted into SortedList in the correct sort order, and the indexing adjusts accordingly. When an element removed, the indexing also adjusts accordingly. Therefore, the index of a specific key-and-value pair might change as elements are added or removed from the SortedList.

Operations on a SortedList tend to be slower than operations on a Hashtable because of the sorting. However, the SortedList offers more flexibility by allowing access to the values either through the associated keys or through the indexes.

A key cannot be a null reference (Nothing in Visual Basic), but a value can be a null reference (Nothing).

Indexes in this collection are zero-based.

The foreach statement of the C# language (for each ...next in Visual Basic) requires the type of each element in the collection. Since each element of a collection based on IDictionary is a key-and-value pair, the element type is not the type of the key or the type of the value. Instead, the element type is DictionaryEntry. For example:

foreach ( DictionaryEntry myEntry in myHashtable ) {
   ...
}
  C# VB

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 mySortedList = new SortedList();
      mySortedList.Add ("First", "Hello");
      mySortedList.Add ("Second", "World");
      mySortedList.Add ("Third", "!");

      // Displays the properties and values of the SortedList.
      Response.Write ("mySortedList" );
      Response.Write ("Count: " + mySortedList.Count + "<br>");
      Response.Write ("Capacity: " + mySortedList.Capacity + "<br>");
      Response.Write ("Keys and Values:");
      PrintKeysAndValues ( mySortedList );
   }


   public static void PrintKeysAndValues ( SortedList myList )  {
      Response.Write ("Key, Value") {
      for ( int i = 0; i < myList.Count; i++ )  {         
         Response.Write (myList.GetKey(i) + ", " + myList.GetByIndex(i));
      }
      Response.Write ("<br>");
   }
}
  C# VB

Skip Navigation Links



Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph