EssentialCSharp icon indicating copy to clipboard operation
EssentialCSharp copied to clipboard

Sorted Dictionary or Dictionary?

Open BenjaminMichaelis opened this issue 3 years ago • 0 comments

@MarkMichaelis currently for 17.07 we use dictionary, but there is another listing for sorted dictionary, do we want to use sorted dictionary instead of dictionary?

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_07
{
    using System;
    using System.Collections.Generic;

    public class Program
    {
        public static void Main()
        {
#if !PRECSHARP5
            Dictionary<string, ConsoleColor> colorMap =
                new Dictionary<string, ConsoleColor>
                {
                    ["Error"] = ConsoleColor.Red,
                    ["Warning"] = ConsoleColor.Yellow,
                    ["Information"] = ConsoleColor.Green,
                    ["Verbose"] = ConsoleColor.White
                };
#else
            Dictionary<string, ConsoleColor> colorMap =
                new Dictionary<string, ConsoleColor>
                {
                    {"Error", ConsoleColor.Red },
                    {"Warning", ConsoleColor.Yellow },
                    {"Information", ConsoleColor.Green },
                    {"Verbose", ConsoleColor.White}
                };
#endif

            Print(colorMap);
      }

      private static void Print(
          IEnumerable<KeyValuePair<string, ConsoleColor>> items)
      {
          foreach (KeyValuePair<string, ConsoleColor> item in items)
          {
              Console.ForegroundColor = item.Value;
              Console.WriteLine(item.Key);
          }
        }
    }
}

BenjaminMichaelis avatar Jul 02 '22 02:07 BenjaminMichaelis