TheDeveloperBlog.com

Home | Contact Us

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

C# Static Dictionary

This C# article shows a static Dictionary that tests strings. It provides example code.

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. This Dictionary, existing only once in memory, is efficient.

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. 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.

Based on:

.NET 4.5

C# program that uses static Dictionary

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	// Write pluralized words from static Dictionary
	Console.WriteLine(PluralStatic.GetPlural("game"));
	Console.WriteLine(PluralStatic.GetPlural("item"));
	Console.WriteLine(PluralStatic.GetPlural("entry"));
    }
}

/// <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)
    {
	// Try to get the result in the static Dictionary
	string result;
	if (_dict.TryGetValue(word, out result))
	{
	    return result;
	}
	else
	{
	    return null;
	}
    }
}

Output

games
items
entries

The static Dictionary is encapsulated in a static class. Access to its string keys and values is provided through the public method GetPlural. This code will return the value in the static Dictionary that is found at the key specified.

Note: Please see the article that focuses on an implementation of this pluralization function for another example.

Plurals

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.

Summary. 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.


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