#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

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #1,133 – Initializing a Dictionary with a Collection Initializer

  1. Pingback: Dew Drop – July 8, 2014 (#1810) | Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: