How to iterate a C# dictionary

The following snippet shows an easy way to iterate a C# dictionary:

</p><br />
<p>            Dictionary<int, string> dictionary = new Dictionary<int, string>();</p><br />
<p>   <a href="http://www.immobild.de/wp-content/geometry-homework.html" title="literature review writing">literature review writing</a>          dictionary.Add(1, "Cat");<br /><br />
            dictionary.Add(2, "Dog");<br /><br />
            dictionary.Add(3, "Horse");</p><br />
<p>            foreach (var item in dictionary)<br /><br />
            {<br /><br />
                // do stuff with item<br /><br />
                Console.WriteLine(string.Format("{0} - {1}", item.Key, item.Value));<br /><br />
            }</p><br />
<p>            // Result:<br /><br />
            // 1 - Cat<br /><br />
            // 2 - Dog<br /><br />
            // 3 - Horse</p><br />
<p>

This entry was posted in .NET and tagged , , . Bookmark the permalink.