Dictionary<>
Written by: maffelu , 2009-09-28 18:22:12
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();
}
}
}
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();
}
}
}
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.