Static Dictionary. A static Dictionary can store global data. It lets you quickly look up strings (or other values). It is shared among many instances.
Static info. A static Dictionary, existing only once in memory, is efficient. We can store values in one location, and access them from many methods.
Simple example. Let us begin with a simple example that shows the mechanics of using a static Dictionary. We can access the static "gems" dictionary in both Main, and the Test() method.
using System;
using System.Collections.Generic;
class Program
{
static Dictionary<string, int> _gems = new Dictionary<string, int>();
static void Main()
{
// Add some data to static dictionary.
_gems.Add("diamond", 500);
_gems.Add("ruby", 200);
// Call another method.
Test();
}
static void Test()
{
// Use static dictionary.
_gems.TryGetValue("diamond", out int value);
Console.WriteLine("STATIC DICTIONARY RESULT: {0}", value);
}
}STATIC DICTIONARY RESULT: 500
Complex example. 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.
Detail The Dictionary doesn't need to be created in multiple instances. Only one instance is needed.
Detail Access to the string keys and values is provided through the public method GetPlural.
Detail This code will return the value in the static Dictionary that is found at the key specified.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Write plural words from static Dictionary.
var result = PluralStatic.GetPlural("game");
Console.WriteLine("RESULT: {0}", result);
}
}
/// <summary>/// Contains plural logic in static class [separate file]/// </summary>
static class PluralStatic
{
/// <summary>/// Static string Dictionary example/// </summary>
static Dictionary<string, string> _dict = new Dictionary<string, string>
{
{"entry", "entries"},
{"image", "images"},
{"view", "views"},
{"file", "files"},
{"result", "results"},
{"word", "words"},
{"definition", "definitions"},
{"item", "items"},
{"megabyte", "megabytes"},
{"game", "games"}
};
/// <summary>/// Access the Dictionary from external sources/// </summary>
public static string GetPlural(string word)
{
// Get the result from the static Dictionary.
string result;
if (_dict.TryGetValue(word, out result))
{
return result;
}
else
{
return null;
}
}
}RESULT: games
Notes, storage. 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. A static Dictionary can improve performance.
Tip If one dictionary can be shared among many class instances, often performance will improve.
A summary. Static Dictionaries can store global program data. We saw example code that encapsulates a static Dictionary in a class, which is instantiated only once in the entire program.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.