c# – Generics: Dictionary (aka hashtable)

In c#, dictionaries are the same thing as hashtables.

Dictonaries are a bit like arrays where you can customise the default index numbers to something more meaningful, here’s an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dictionary
{
    class Program
    {
        static void Main(string[] args)
        {
			// here we create an empty dictionary
			// called "CountriesAndCapitals"
			// where we define that
			// the index, is a string, and the main value is
			// also a string:
			Dictionary<string, string=> CountriesAndCapitals = new Dictionary<string, string=>();

			// Here we add items to the dictionary
			CountriesAndCapitals.Add("England","London");
			CountriesAndCapitals.Add("France","Paris");
			CountriesAndCapitals.Add("Italy","Rome");

			Console.WriteLine(CountriesAndCapitals["Italy"]);
		}
    }
}

Dictionaries are really powerful, and one of things it often gets used for is to create a dictionary where the key is string, but the value itself is actually a custom object, and to instantiate this type of object we do:

Dictionary {Dictionary’s Name} = new Dictionary();

Now something that is common practice is that we create a custom class, which we’ll use to create the custom objects needed to populate the dictionary. A (static) method (that gets triggered by the main program), which in turn instantiates the dictionary object, the method then instantiates a number of objects from the custom class and adds them to the dictionary along with a key. Finally the method returns the dictionary back to it’s caller, which would capture it in a dictionary-variable.

Here is an example of this in action:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dictionary
{
    class Program
    {
        static void Main(string[] args)
        {

            // Here we call the CountryStat's class's (static) GetCountryListInfo method,
            // in order for it to generate the dictionary, populate it, and 
            // then return the dictionary:
            var RetrievedCountryInfo = CountryStat.GetCountryListInfo();

            Console.WriteLine
			(
				"England has a population of {0} and it's official language is {1}. It's capital city is called {2}.",
				RetrievedCountryInfo["England"].Population,
				RetrievedCountryInfo["England"].
				Language,RetrievedCountryInfo["England"].Capital
			);
        }
    }

    public class CountryStat
    {

        public string Capital { get; set; }
        public int Population { get; set; }
        public string Language { get; set; }

        public CountryStat(string Capital, int Population, string Language)
        {
            this.Capital = Capital;
            this.Population = Population;
            this.Language = Language;
        }

        // Here we instantiate the dictionary, and add
        // entries to the dictionary
        public static Dictionary<string, CountryStat=> GetCountryListInfo()
		{
			// Here we create the empty dictionary that 
			// we will populate:
			Dictionary<string, CountryStat=> DictionaryOfCountries = new Dictionary<string, CountryStat=>();
			
			// Here we create the first object to be added to the dictionary:
			CountryStat EnglandObject = new CountryStat("London", 60000000, "English");
			// Now we add this object to the dictionary:
			DictionaryOfCountries.Add("England",EnglandObject);

			// Here we create the next object to be added to the dictionary:
			CountryStat FranceObject = new CountryStat("Paris", 90000000, "French");
			// Now we add this object to the dictionary:
			DictionaryOfCountries.Add("France",FranceObject);
			
			
			// Here we create the next object to be added to the dictionary:
			CountryStat RussiaObject = new CountryStat("Moscow", 180000000, "Russian");
			// Now we add this object to the dictionary:
			DictionaryOfCountries.Add("Russia",RussiaObject);
			
			// Now we return the populated dictionary back to the caller:
			return DictionaryOfCountries;		
		}
    }
}