To iterate a dictionary in C# and VB.NET you can use the following snippet.

Sample C#

var myDictionary = new Dictionary<int, string>();
myDictionary.Add(10, "Value1");
myDictionary.Add(20, "Value2");
myDictionary.Add(30, "Value3");

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

Sample VB.NET

Dim myDictionary = New Dictionary(Of Integer, String)()
myDictionary.Add(10, "Value1")
myDictionary.Add(20, "Value2")
myDictionary.Add(30, "Value3")

For Each myPair As KeyValuePair(Of Integer, String) In myDictionary
	Console.WriteLine("{0} - {1}", myPair.Key, myPair.Value)
Next

for more informations see KeyValuePair

One thought on “How to iterate a dictionary in C# and VB.NET”

Leave a Reply