TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# ListDictionary

This C# page shows how to use the ListDictionary type. It provides a simple benchmark.

ListDictionary. A ListDictionary is a specialized collection.

It is found in the System.Collections.Specialized namespace. This type represents a non-generic dictionary type. It is implemented with a linked list.

Example. The ListDictionary can be used in basically the same way as the Hashtable. You can Add and Remove elements and use the Count property. The key difference is in its performance characteristics.

Tip: In small collections, it will be faster than a Hashtable with the same data.

Hashtable

C# program that uses ListDictionary

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("deves", 3);

	Hashtable hash = new Hashtable();
	hash.Add("dot", 1);
	hash.Add("net", 2);
	hash.Add("deves", 3);

	Console.WriteLine("ListDictionary.Count: {0}", list.Count);
	Console.WriteLine("Hashtable.Count: {0}", hash.Count);
	Console.WriteLine();

	const int max = 10000000;
	var s1 = Stopwatch.StartNew();
	for (int i = 0; i < max; i++)
	{
	    object a = list["deves"];
	}
	s1.Stop();
	var s2 = Stopwatch.StartNew();
	for (int i = 0; i < max; i++)
	{
	    object a = hash["deves"];
	}
	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"));
    }
}

Output

ListDictionary.Count: 3
Hashtable.Count: 3

ListDictionary: 17.69 ns
Hashtable: 51.14 ns

You can see that the ListDictionary was faster for looking up an element in a small collection. The Hashtable was slower. If the size of the collections was expanded dramatically, you would see that the Hashtable would become much faster.

So: The Hashtable's time would remain fairly constant while the ListDictionary's time would linearly increase.

Discussion. I have spent a lot of time trying to reduce times in C# programs. I have found that types in System.Collections.Generic are much better overall than those in System.Collections and System.Collections.Specialized.

Tip: I recommend people never write new programs with types such as ListDictionary and Hashtable.

And: As for implementing Dictionary collections with Lists, this can improve performance.

But: This raises the possibility of a grossly degenerate performance situation if your program needs to process much more data.

Therefore, use List when you need to store but not search for elements. And use Dictionary when lookups are required. If you need to add or remove elements, Dictionary is better—these operations require less memory access.

ListDictionary

Summary. The ListDictionary collection is not something most developers should use. It may introduce degenerate behavior into your program unless you are careful with it. And its performance benefits are at best minimal.

Tip: Modern types from System.Collections.Generic are better in nearly all programs.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf