ListDictionary
This is a specialized C# collection. It is found in the System.Collections
Specialized namespace. It is similar to Hashtable
.
This type represents a non-generic dictionary type. It is implemented with a linked list. It may be faster for when we have many small collections.
With ListDictionary
, we can Add and Remove
elements and use the Count
property. The key difference is in its performance characteristics.
Hashtable
with the same data.ListDictionary
and looks up elements from it.Hashtable
instead of a ListDictionary
.ListDictionary
was faster for looking up an element in a small collection. The Hashtable
was slower.using System; using System.Collections; // For Hashtable. using System.Collections.Specialized; // For ListDictionary. using System.Diagnostics; class Program { static void Main() { ListDictionary list = new ListDictionary(); list.Add("dot", 1); list.Add("net", 2); list.Add("perls", 3); Hashtable hash = new Hashtable(); hash.Add("dot", 1); hash.Add("net", 2); hash.Add("perls", 3); Console.WriteLine("ListDictionary.Count: {0}", list.Count); Console.WriteLine("Hashtable.Count: {0}", hash.Count); Console.WriteLine(); const int max = 10000000; // Version 1: use ListDictionary. var s1 = Stopwatch.StartNew(); for (int i = 0; i < max; i++) { object a = list["perls"]; } s1.Stop(); // Version 2: use Hashtable. var s2 = Stopwatch.StartNew(); for (int i = 0; i < max; i++) { object a = hash["perls"]; } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / max).ToString("ListDictionary: 0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / max).ToString("Hashtable: 0.00 ns")); } }ListDictionary.Count: 3 Hashtable.Count: 3 ListDictionary: 17.69 ns Hashtable: 51.14 ns
I have found that types in System.Collections
Generic are much better overall than those in System.Collections
and System.Collections
Specialized.
ListDictionary
and Hashtable
.Dictionary
collections with Lists, this can be fast on small amounts of data, but will be slow on larger amounts.The ListDictionary
collection has performance benefits in certain programs. But modern types from System.Collections
Generic are better in nearly all programs.