What is the best way to iterate over a Dictionary in C#?

Dictionary < TKey, TValue > Class

c# dictionary

The Dictionary < TKey, TValue > class is a generic class and you can store any Data Types. The C# Dictionary Add method takes two parameters, which mapping from a set of keys to a set of values. Each insertion to the Dictionary consists of a Key and its associated Value

Dictionary Add

public void Add(TKey key,TValue value)

Dictionary myDictionary = new Dictionary();
myDictionary.Add("Sunday", 1);
myDictionary.Add("Monday", 2);
myDictionary.Add("Tuesday", 3);
myDictionary.Add("Wednesday", 4);
myDictionary.Add("Thursday", 5);
myDictionary.Add("Friday", 6);
myDictionary.Add("Saturday", 6);

Retrieve Values from C# Dictionary

c# iterate over a Dictionary

There are many different ways to iterate over a Dictionary in C#

Retrieve C# Dictionary Kay and Value

foreach (KeyValuePair item in myDictionary)
{
    MessageBox.Show(item.Key + "   " + item.Value);
}

VB.Net

For Each item As KeyValuePair(Of String, Integer) In myDictionary
	MessageBox.Show(item.Key + "   " + item.Value)
Next

Retrieve C# Dictionary Key only

foreach (var key in myDictionary.Keys)
{
    MessageBox.Show(key);
}

Retrieve C# Dictionary Value only

foreach (var value in myDictionary.Values)
{
    MessageBox.Show(value.ToString());
}

Using for loop

c# dictionary LINQ

You can use for loop also because sometimes you need a counter value. In that case you should use ElementAt() method that provides by LINQ. In this case you should, using System.Linq;

for (int count = 0; count < myDictionary.Count; count++)
{
    var element = myDictionary.ElementAt(count);
    var Key = element.Key;
    var Value = element.Value;
    MessageBox.Show(Key + "  " + Value);
}

VB.Net

For count As Integer = 0 To myDictionary.Count - 1
	Dim element = myDictionary.ElementAt(count)
	Dim Key = element.Key
	Dim Value = element.Value
	MessageBox.Show(Key + "  " + Value)
Next

How to C# Dictionary

A Dictionary class is a data structure that represents a collection of keys and values pair of data. The key is identical in a key-value pair and it can have at most one value in the dictionary, but a value can be associated with many different keys. More about.... C# Dictionary

Dictionary Versus HashTable

C# Dictionary and HashTable

Dictionary is a generic type, Hashtable is not. That means you get type safety with Dictionary, because you can't insert any random object into it, and you don't have to cast the values you take out. Since both Dictionary and Hashtable are internally hashtables, so fast access to many-item data according to key, also both need immutable and unique keys. More about.... Dictionary Vs HashTable

Dictionary Versus List

C# Dictionary and List

Both lists and dictionaries are used to store collections of data. The Dictionary is based on a hash table, that means it uses a hash lookup, which is a rather efficient algorithm to look up things, on the other hand, a list you have to go element by element until it finds the result from beginning to the result each time. More about.... Dictionary vs List

NEXT.....String to byte Array