56. Roman numerals

There are many ways that you could approach this problem. I decided to use lookup tables to make things faster and simpler.

The following code shows a string extension method that converts a Roman numeral string into a long integer:

// Maps letters to numbers.private static Dictionary<char, long> LetterValues = null;// Convert Roman numerals into an integer.public static long ToArabic(this string roman){    // Initialize the letter lookup table if    // it hasn't been initialized yet.    if (LetterValues == null)    {        LetterValues = new Dictionary<char, long>();        LetterValues.Add('I', 1);        LetterValues.Add('V', 5);        LetterValues.Add('X', 10);        LetterValues.Add('L', 50);        LetterValues.Add('C', 100);        LetterValues.Add('D', 500); LetterValues.Add('M', ...

Get The Modern C# Challenge now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.