Represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index.
public class SortedList: Object, IDictionary, ICloneable
ObjectSortedList
// 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( "Pineapple", 1.49 );
// 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.
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
*/
Namespace System.Collections Flash Library corlib.scl Flash Library Version 2.0.0.2466 Silverlight Library System.Windows.Forms.dll
| © 2003-2007 NETiKA Technologies. All rights reserved. |