Home
Map
StringDictionaryUse the StringDictionary collection type for string keys and string values.
C#
This page was last reviewed on Jul 12, 2022.
StringDictionary. This is a specialized collection. It is found in the System.Collections Specialized namespace. It only allows string keys and string values.
Some drawbacks. The StringDictionary type is limited. It also suffers from performance problems. Usually the Dictionary generic type is a better choice.
Dictionary
This program shows some methods on the StringDictionary. The constructor is called, and then Add is invoked. Various other features of the type are then used.
Constructor
Also The Keys and Values properties are used in foreach-loops. The Remove, Count and Clear methods are demonstrated.
foreach
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); } }
feline True True False True False False dog = canine cat = feline dog cat canine feline 1 0
Internals. StringDictionary 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.
Hashtable
Also Every time you access a string key, it is converted to lowercase, which allocates on the managed heap.
String ToLower, ToUpper
Info Other than this, performance is roughly the same as a Hashtable. Because of these shortcomings, detailed benchmarks are not useful.
Benchmark
Tip For better performance, use a Dictionary generic type with string keys. And prefer the StringComparer.Ordinal option.
StringComparison
Summary. The StringDictionary is mostly useless. Its underlying collection, the Hashtable, is also obsolete because of the faster and cleaner Dictionary generic type.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jul 12, 2022 (rewrite).
Home
Changes
© 2007-2024 Sam Allen.