Implements IDictionary by using a ListDictionary while the collection is small, and then switching to a Hashtable when the collection gets large.
public class HybridDictionary: Object, IDictionary, ICollection, IEnumerable
ObjectHybridDictionary
// This sample shows how to implement the HybridDictionary class. It also shows how to use the Add and CopyTo methods.
using System.Diagnostics;
using System.Collections;
using System.Collections.Specialized;
public class SampleHybridDictionary
{
static void Main()
{
// Creates a new HybridDictionary.
HybridDictionary myHybridDict = new HybridDictionary();
string[] targetArray = new string[6];
// Adds the new entries to the HybridDictionary.
myHybridDict.Add(1,"January");
myHybridDict.Add(2,"February");
// Displaying.
Debug.WriteLine("Before copying");
Debug.Write("\tThe targetArray: "); DisplayArray(targetArray
);Debug.WriteLine("\n\t" + "KEY \tVALUE"); DisplayHybridDict(myHybridDict);
// Copies the values of the HDictionary to an array.
myHybridDict.Values.CopyTo(targetArray, 0);
// Displaying.
Debug.WriteLine("\nAfter copying");
Debug.Write("\tThe targetArray: "); DisplayArray(targetArray);
Debug.WriteLine("\n\t" + "KEY \tVALUE"); DisplayHybridDict(myHybridDict);
}
static void DisplayArray(string[] myArray)
{
for(int i = 0; i < myArray.Length; i++)
Debug.Write("\t" + myArray[i]);
}
static void DisplayHybridDict(IDictionary myDict)
{
IDictionaryEnumerator myIDE = myDict.GetEnumerator();
Debug.WriteLine("\t" + myIDE.Key + "\t\t" + myIDE.Value);
}
}
/*
The output of the sample.
Before copying
The targetArray: null null null null null null
KEY VALUE
1 January
2 February
3 March
4 April
5 May
6 June
After copying
The targetArray: January February March April May June
KEY VALUE
1 January
2 February
3 March
4 April
5 May
6 June
*/
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. |