GOA - System.Collections.SortedList

SortedList Class


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

Definition

public class SortedList: Object, IDictionary, ICloneable

Members Table

MethodDescription
AddAdds an element with the specified key and value to the SortedList.
ClearRemoves all elements from the SortedList.
CloneCreates a shallow copy of the SortedList.
ContainsDetermines whether the SortedList contains a specific key.
ContainsKeyDetermines whether the SortedList contains a specific key.
ContainsValueDetermines whether the SortedList contains a specific value.
CopyToCopies the SortedList elements to a Array instance at the specified index.
EqualsOverloaded. Compares two objects to determine if they are equal. (Inherited from Object)
GetByIndexGets the value at the specified index of the SortedList.
GetEnumeratorOverloaded. (Inherited from IEnumerable)
GetHashCodeServes as a hash function for a particular type, suitable for use in hashing algorithms and data structures like a hash table. (Inherited from Object)
GetKeyGets the key at the specified index of the SortedList.
GetKeyListGets the keys in the SortedList.
GetTypeGets the Type of the current instance. (Inherited from Object)
GetValueListGets the values in the SortedList.
IndexOfKeyReturns the index of the specified key in the SortedList.
IndexOfValueReturns the index of the first occurrence of the specified value in the SortedList.
ReferenceEqualsDetermines whether the specified Object instances are the same instance. (Inherited from Object)
RemoveRemoves the element with the specified key from SortedList.
RemoveAtRemoves the element at the specified index of SortedList.
SetByIndexReplaces the value at a specific index in the SortedList.
SortedListOverloaded. Initializes a new instance of SortedList.
SynchronizedReturns a synchronized wrapper for the SortedList.
System.Collections.IEnumerable.GetEnumeratorReturns an IEnumerator that can iterate through a SortedList.
ToStringReturns a String that represents the current Object. (Inherited from Object)
TrimToSizeSets the capacity to the actual number of elements in the SortedList.
this[]Gets or sets the value associated with a specific key in the SortedList.
PropertyDescription
CapacityGets or sets the capacity of the SortedList.
CountGets the number of elements contained in the SortedList.
IsFixedSizeGets a value indicating whether the SortedList has a fixed size.
IsReadOnlyGets a value indicating whether the SortedList is read-only.
IsSynchronizedGets a value indicating whether access to the SortedList is synchronized.
KeysGets the keys in the SortedList.
SyncRootGets an object that can be used to synchronize access to the SortedList.
ValuesGets the values in the SortedList.

Inheritance Hierarchy

Object
SortedList

Examples

// This examples shows how to create and initialize a SortedList.

// It shows also some properties and method of the SortedList class.

 

using System;

using System.Diagnostics;

using System.Collections;

 

public class SampleSortedList

{

     static void Main()

     {

          // Creates and initializes a new sorted list.

          SortedList mySL= new SortedList();

 

          // Adds elements to the sorted list.

          mySL.Add( "Apple", 1.49 );

          mySL.Add( "Banana", 1.29 );

          mySL.Add( "Pineapple", 1.49 );

          mySL.Add( "Kiwi", 1.69 );

          mySL.Add( "Orange", 0.89 );

          mySL.Add( "Lemon", 0.99 );

 

          // Displays the contents of the sorted list.

          Debug.WriteLine("Initial contents of the sorted list:");

          DisplayValues4(mySL);

 

          // Searches the Hashtable with Contains method.

          bool t =mySL.Contains("Banana");

          Debug.WriteLine("\n"+@"Contains ""Banana""(using the Contains method):"+t.ToString());

 

          bool t1 =mySL.ContainsKey("Orange");

          Debug.WriteLine("\n"+@"Contains ""Orange""(using the ContainsKey method):"+t1.ToString());

 

          bool t2 =mySL.ContainsValue(1.29);

          Debug.WriteLine("\n"+@"Contains ""1.29""(using the ContainsValue method):"+t2.ToString());

 

          int index = mySL.IndexOfKey("Lemon");

          Debug.WriteLine("\n"+@"Index of key ""Lemon"":"+index.ToString());

 

          int index1 = mySL.IndexOfValue(1.69);

          Debug.WriteLine("\n"+@"Index of value ""1.69"":"+index1.ToString());

 

          // Removes an element of the sorted list.

          mySL.Remove("Kiwi");

          Debug.WriteLine("\n"+@"Sorted list after removing ""Kiwi"":");

          DisplayValues4(mySL);

 

          // Creates and initializes a new array of string.

          Array myStringArray = new string[7] {"abc", "def", "ghi", "jkl", "mno","pqr", "stu"};

 

          // Displays the content of an array of string.

          Debug.WriteLine("\nContents of the array of string:");

          DisplayArrayValues(myStringArray);

 

          // Copies the keys of the SortedList into the array.

          mySL.Keys.CopyTo(myStringArray, 0);

 

          // Display of the content of the array after copying the keys of the SortedList in it.

          Debug.WriteLine("\nContents of the array after copying the keys of the SortedList in it:");

          DisplayArrayValues(myStringArray);

     }

 

     // Displays values using Keys property and the this[] method.

     public static void DisplayValues( SortedList mySortedList )

     {

          foreach ( DictionaryEntry de in mySortedList )

               Debug.WriteLine("\t"+de.Key.ToString()+":"+de.Value.ToString());

     }

 

     // Displays values using an enumerator.

     public static void DisplayValues2( SortedList myDictionary )

     {

          DictionaryEntry myDE;

          System.Collections.IEnumerator myEnumerator = myDictionary.GetEnumerator();

          while ( myEnumerator.MoveNext() )

               if ( myEnumerator.Current != null )

                    myDE = (DictionaryEntry) myEnumerator.Current;

                    Debug.WriteLine("\t"+myDE.Key.ToString()+":"+myDE.Value.ToString());

     }

 

     // Displays values using the Count property.

     public static void DisplayValues3( SortedList myList )

     {

          for ( int i = 0; i < myList.Count; i++ )

          {

               Debug.WriteLine("\t" +myList.GetKey(i)+":"+myList.GetByIndex(i));

          }

     }

 

     // Displays values using IList.

     public static void DisplayValues4( SortedList mySortedList )

     {

          IList myKeyList = mySortedList.GetKeyList();

          IList myValueList = mySortedList.GetValueList();

               for ( int i = 0; i < mySortedList.Count; i++ )

                    Debug.WriteLine("\t"+ myKeyList[i]+":"+myValueList[i]);

     }

 

     // Displays values of an array.

     public static void DisplayArrayValues ( Array array )

     {

          for ( int i = 0; i < array.Length; i++ )

          {

               Debug.WriteLine("\t"+array.GetValue(i)+" ");

          }

     }

}

 

/*

 

The output of the sample.

 

Initial contents of the sorted list:

     Apple:1.49

     Banana:1.29

     Kiwi:1.69

     Lemon:0.99

     Orange:0.89

     Pineapple:1.49

 

Contains "Banana"(using the Contains method):True

Contains "Orange"(using the ContainsKey method):True

Contains "1.29"(using the ContainsValue method):True

Index of key "lemon":3

Index of value "1.69":2

 

Sorted list after removing "Kiwi":

     Apple:1.49

     Banana:1.29

     Lemon:0.99

     Orange:0.89

     Pineapple:1.49

 

Contents of the array of string:

     abc

     def

     ghi

     jkl

     mno

     pqr

     stu

 

Contents of the array after copying the keys of the SortedList in it:

     Apple

     Banana

     Lemon

     Orange

     Pineapple

     pqr

     stu

 

*/

Class Information

NamespaceSystem.Collections
Flash Librarycorlib.scl
Flash Library Version2.0.0.2466
Silverlight LibrarySystem.Windows.Forms.dll



© 2003-2007 NETiKA Technologies. All rights reserved.