#1,133 – Initializing a Dictionary with a Collection Initializer

You can initialize a genericĀ Dictionary with a collection initializer as follows:

            // Initialization with collection initializer
            Dictionary<string, int> guys = new Dictionary<string,int> {
                    {"Galileo", 1564},
                    {"Magellan", 1480},
                    {"Voltaire", 1694},
                    {"Kepler", 1571},
                    {"Keaton", 1895}
                };

            foreach (KeyValuePair<string, int> kvp in guys)
                Console.WriteLine("{0} ({1})", kvp.Key, kvp.Value);

1133-001

Advertisements