MorkaLork Development

Interesting stuff I've picked up over the years...

Dictionary<>

2009-09-28 18:22:12 | 246 views | Dictionary csharp key value

Dictionary


The Dictionary class works just like the HashTable but is generic which means it’s a bit more versatile. You have to enter a key for each value which is what you will also have to use when you want to extract a value.
This is how a Dictionary collection could be used:



using System;
using System.Collections.Generic;

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

Dictionary<int, string> httpStatusCode = new Dictionary<int, string>();
httpStatusCode[100] = "Informational";
httpStatusCode[200] = "Success";
httpStatusCode[300] = "Redirection";
httpStatusCode[400] = "Client Error";
httpStatusCode[500] = "Server Error";

//Output via a foreach loop
Console.WriteLine("A list of HTTP Status Codes

");

Console.WriteLine("{0, -6}| {1, -25}", "Code", "Description");
Console.WriteLine("----------------------");
foreach (KeyValuePair<int, string> kvp in httpStatusCode) {
Console.WriteLine("{0, -6}| {1, -25}", kvp.Key, kvp.Value);
}
Console.WriteLine("

");

//Selecting individual items
int statusCode = 200;
String statusDescription = httpStatusCode[statusCode];

Console.WriteLine("Status code {0} means {1}.", statusCode, statusDescription);

Console.Read();
}
}
}


This little application will display the various Http status code ranges. This will be the output:

The dictionary in action

As we can see, we store the status code as key and the description as value. So if you have a error 404, you can just check the dictionary for the 400 status code to get an idea of what went wrong.


Usage


The Dictionary collection is generally used for storing linked data, such as data regarding country telephone codes. This data comes in pairs; the country code and the country:



using System;
using System.Collections.Generic;

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

Dictionary<int, string> countryCodes = new Dictionary<int, string>();

countryCodes.Add(1, "Canada/US");
countryCodes.Add(7, "Kazakhstan");
countryCodes.Add(20, "Egypt");
countryCodes.Add(27, "South Africa");
countryCodes.Add(30, "Greece");
countryCodes.Add(31, "Netherlands");
countryCodes.Add(32, "Belgium");
countryCodes.Add(33, "France");
countryCodes.Add(34, "Spain");
countryCodes.Add(36, "Hungary");
//And so on...

while (true) {
Console.Write("Enter a country code: ");

int input;
bool checkInput = int.TryParse(Console.ReadLine(), out input);

if (checkInput) {
if (countryCodes.ContainsKey(input)) {
Console.WriteLine("Country: {0}", countryCodes[input]);
}
else {
Console.WriteLine("{0} is not a valid country code!", input);
}
}
else
Console.WriteLine("Make sure that the country code is numeric!");

Console.WriteLine();
}

Console.Read();
}
}
}


The country codes could be fetched from a database into the Dictionary object and then used. A user would see this:

The country code application in action

This is just an example of how useful the Dictionary collection can be.



Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.