Implements IDictionary using a singly linked list. Recommended for collections that typically contain 10 items or less.
public class ListDictionary: Object, IDictionary, ICollection, IEnumerable
ObjectListDictionary
// This examples shows how to create and initialize a ListDictionary.
// It shows also some properties and method of the ListDictionary class.
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Specialized;
public class SampleListDictionary
{
static void Main()
{
// Creates and initializes a new ListDictionary.
ListDictionary myLD = new ListDictionary();
// Adds elements to the ListDictionary.
// Displays the contents of the ListDictionary.
Debug.WriteLine("\nInitial contents of the List dictionary(key:value):");
DisplayValues4(myLD
);
// Searches the ListDictionary with Contains method.
Debug.WriteLine("\n"+@"Contains ""Hello"":" +myLD.Contains("Hello"));
// Removes an element of the list dictionary.
Debug.WriteLine("\n"+@"Contents of the List dictionary after removing the element having for key ""This"" (key:value):");
DisplayValues(myLD);
// Creates and initialize an arry of string.
string[] myStringArray = new string[6] {"abc", "def", "ghi", "jkl", "mno","pqr"};
Debug.Write("\nInitial contents of an array of string:");
DisplayArrayValues(myStringArray);
// Copies the keys of the ListDictionary to the array.
myLD.Keys.CopyTo(myStringArray,0);
Debug.Write("\n\nContents of the array after copying the keys of the ListDictionary in it:");
DisplayArrayValues(myStringArray);
}
// Displays values using Keys property and the this[] method.
public static void DisplayValues( ListDictionary myListDictionary )
{
ICollection myKeys = myListDictionary.Keys;
foreach ( string k in myKeys )
Debug.WriteLine("\t"+k+":"+myListDictionary[k].ToString());
}
// Displays values using an enumerator.
public static void DisplayValues2( ListDictionary myListDictionary )
{
DictionaryEntry myDE;
System.Collections.IEnumerator myEnumerator = myListDictionary.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 foreach statement.
public static void DisplayValues3 ( ListDictionary myListDictionary)
{
foreach ( DictionaryEntry myDE in myListDictionary )
Debug.WriteLine("\t"+myDE.Key.ToString()+":"+myDE.Value.ToString());
}
// Uses the Keys, Values, Count properties and this[] method.
public static void DisplayValues4( ListDictionary myListDictionary )
{
string[] myKeys = new string[myListDictionary.Count];
myListDictionary.Keys.CopyTo( myKeys, 0 );
double[] myValues = new double[myListDictionary.Count];
myListDictionary.Values.CopyTo( myValues, 0);
for ( int i = 0; i < myListDictionary.Count; i++ )
Debug.WriteLine( "\t"+myKeys[i]+":"+myValues[i] );
}
// Displays values of an array.
public static void DisplayArrayValues ( Array array )
{
for ( int i = 0; i < array.Length; i++ )
Debug.Write(array.GetValue(i)+" ");
}
}
/*
The output of the sample.
Initial contents of the List dictionary(key:value):
Hello:0.99
This:1.49
is:1.29
a:1.69
Test:1.29
!:0.89
Contains "Hello":True
Contents of the List dictionary after removing the element having for key "This"(key:value):
Hello:0.99
is:1.29
a:1.69
Test:1.29
!:0.89
Initial contents of an array of string:abc def ghi jkl mno pqr
Contents of the array after copying the keys of the ListDictionary in it:Hello is a Test ! pqr
*/
Namespace System.Collections.Specialized Flash Library System.scl Flash Library Version 2.0.0.1308 Silverlight Library System.Windows.Forms.dll
| © 2003-2007 NETiKA Technologies. All rights reserved. |