Specialized Dictionary

Overview

  • Special case dictionaries
  • ListDictionary
    • Used as a HashTable for fewer than 10 items.
    • Implemented as a simple linked list behind the scenes.
    • Same interface as HashTable, easy to replace.
  • HybridDictionary
    • Used as a HashTable for unknown sizes of items.
    • Implemented as a combination between HashTable and ListDictionary.
    • Switches over to a HashTable when the collection becomes too big.
    • Same interface as HashTable, easy to replace.
  • OrderedDictionary
    • Used as a HashTable when you need access to the index.
    • A plain dictionary mapping, like a cross between HashTable and ArrayList.

Good practice

Examples

ListDictionary: Adding and Removing Elements

ListDictionary a = new ListDictionary();
a.Add("Key2", "Hello");
a.Add("Key1", "World");
a.Add("Key0", "Start");
 
Console.WriteLine(a["Key1"] + " " + a["Key2"]);
Console.WriteLine();
foreach (DictionaryEntry d in a) { Console.WriteLine(d.Value); }
Console.WriteLine();
foreach (var v in a.Values) { Console.WriteLine(v); }
Console.WriteLine();
a.Remove("Key1");
foreach (var k in a.Keys) { Console.WriteLine(a[k]); }

HybridDictionary: Adding and Removing Elements

HybridDictionary a = new HybridDictionary();
a.Add("Key2", "Hello");
a.Add("Key1", "World");
a.Add("Key0", "Start");
 
Console.WriteLine(a["Key1"] + " " + a["Key2"]);
Console.WriteLine();
foreach (DictionaryEntry d in a) { Console.WriteLine(d.Value); }
Console.WriteLine();
foreach (var v in a.Values) { Console.WriteLine(v); }
Console.WriteLine();
a.Remove("Key1");
foreach (var k in a.Keys) { Console.WriteLine(a[k]); }

OrderedDictionary: Adding and Removing Elements

OrderedDictionary a = new OrderedDictionary();
a.Add("Key1", "Hello");
a.Add("Key2", "World");
a.Add("Key0", "Start");
a.Insert(1, "Key10", "Inserted");
 
Console.WriteLine(a["Key1"] + " " + a["Key2"]);
Console.WriteLine();
foreach (DictionaryEntry d in a) { Console.WriteLine(d.Value); }
Console.WriteLine();
foreach (var v in a.Values) { Console.WriteLine(v); }
Console.WriteLine();
a.Remove("Key1");
a.RemoveAt(0);
foreach (var k in a.Keys) { Console.WriteLine(a[k]); }