C# Static DictionaryDevelop a static Dictionary that tests strings. Compute plural words from singular words.
A static Dictionary can store global data. It lets you quickly look up strings (or other values). It is shared among many instances. This Dictionary, existing only once in memory, is efficient.DictionaryStatic
The example here is useful code for finding a plural version of a word. You can pass in a string, and the method will return the pluralized version. The Dictionary is static because it doesn't require state.
Also: The Dictionary doesn't need to be created in multiple instances. Only one instance is needed.
GetPlural: Access to the string keys and values is provided through the public method GetPlural.Public, private
Example: This code will return the value in the static Dictionary that is found at the key specified.
C# program that uses static Dictionary
static void Main()
// Write pluralized words from static Dictionary
/// Contains plural logic in static class [separate file]
static class PluralStatic
/// Static string Dictionary example
static Dictionary<string, string> _dict = new Dictionary<string, string>
/// Access the Dictionary from external sources
public static string GetPlural(string word)
// Try to get the result in the static Dictionary
if (_dict.TryGetValue(word, out result))
A static Dictionary
can be stored in a non-static class. In this case, it will still be tied to the type, not the instance. Sometimes, using a static Dictionary is a good performance optimization.
Tip: If one dictionary can be shared among many class instances, often performance will improve.
We saw example code that encapsulates a static Dictionary in a class, which is instantiated only once in the entire program. You could extend the code to have entries added at runtime, just like a normal Dictionary.
Review: The discussion focuses on the static Dictionary. It points to a more complete word pluralization lookup table.
© 2007-2020 Sam Allen. Send bug reports to email@example.com.