C# StringDictionary

String type

You are curious about the StringDictionary collection in the System.Collections.Specialized namespace in the C# language, or for some reason want to actually use it. As a collection that only allows string keys and string values, the StringDictionary is limited. It also suffers from performance problems.

Example

First, this program demonstrates some of the methods on the StringDictionary type. The constructor is called, and then the Add method is invoked; the indexer is tested; the ContainsKey and ContainsValue methods are used; the IsSynchronized property is accessed; the enumerator is used; the Keys and Values properties are used in foreach loops; and finally the Remove, Count, and Clear methods are demonstrated.

This C# example program uses the StringDictionary collection type.

Program that uses StringDictionary [C#]

using System;
using System.Collections;
using System.Collections.Specialized;

class Program
{
    static void Main()
    {
	// Create new StringDictionary.
	StringDictionary dict = new StringDictionary();
	dict.Add("cat", "feline");
	dict.Add("dog", "canine");

	// Try the indexer.
	Console.WriteLine(dict["cat"]);
	Console.WriteLine(dict["test"] == null);

	// Use ContainsKey.
	Console.WriteLine(dict.ContainsKey("cat"));
	Console.WriteLine(dict.ContainsKey("puppet"));

	// Use ContainsValue.
	Console.WriteLine(dict.ContainsValue("feline"));
	Console.WriteLine(dict.ContainsValue("perls"));

	// See if it is thread-safe.
	Console.WriteLine(dict.IsSynchronized);

	// Loop through pairs.
	foreach (DictionaryEntry entry in dict)
	{
	    Console.WriteLine("{0} = {1}", entry.Key, entry.Value);
	}

	// Loop through keys.
	foreach (string key in dict.Keys)
	{
	    Console.WriteLine(key);
	}

	// Loop through values.
	foreach (string value in dict.Values)
	{
	    Console.WriteLine(value);
	}

	// Use Remove method.
	dict.Remove("cat");
	Console.WriteLine(dict.Count);

	// Use Clear method.
	dict.Clear();
	Console.WriteLine(dict.Count);
    }
}

Output

feline
True
True
False
True
False
False
dog = canine
cat = feline
dog
cat
canine
feline
1
0

Implementation

.NET Framework information

How is the StringDictionary type internally implemented? It simply uses a Hashtable member field to store all the data you add. The Hashtable is not a generic type so it will have some overhead; also every time you look up a key the key is converted to lowercase, adding more overhead.

Performance analysis

Garbage collection visualization

This section describes the performance characteristics of the StringDictionary. Every time you access a string key, it is converted to lowercase, which allocates on the managed heap. Other than this, the performance is the same as a Hashtable roughly. Because of these shortcomings, detailed benchmarks are not useful. For better performance, use a Dictionary generic type with string keys.

Dictionary Examples

Summary

Warning

The StringDictionary does not seem to have many unique advantages, or even many advantages at all in modern C# programs. Its underlying collection, the Hashtable, is also obsolete because of the faster and cleaner Dictionary generic type. In addition, StringDictionary imposes further overhead and restrictions on the Hashtable, leading to a collection that is probably useless in almost all programs and should be avoided.

Collections
.NET