I've seen a few different ways to iterate over a Dictionary in C#. Is there a standard way?
It entirely depends on what you're looking for. There are collections for both the Keys and the Values so that you can iterate over either one depending your your needs.
foreach(KeyValuePair<String,String> entry in MyDic)
{
// do something with entry.Value or entry.Key
}
I would say foreach is the standard way, though it obviously depends on what you're looking for
foreach(var value in my_dictionary) {
...
}
Is that what you're looking for?
If you are trying to use a generic Dictionary in C# like you would use an associative array in another language:
foreach(KeyValuePair<string, string> item in myDictionary)
{
foo(item.Key);
bar(item.Value);
}
Or, if you only need to iterate over the collection of keys, use
foreach(var item in myDictionary.Keys)
{
foo(item);
}
And lastly, if you're only interested in the values:
foreach(var item in myDictionary.Values)
{
foo(item);
}
(Take note that the var
keyword is an optional C# 3.0 feature, you could also use the exact type of your keys/values here)
There are plenty of options. My personal favorite is by KeyValuePair
Dictionary<string,object> myDictionary = new Dictionary<string,object>();
//Populate your dictionary here
Foreach (KeyValuePair<string,object> kvp in myDictionary)
{
//Do some interesting things;
}
You can also use the Keys and Values Collections
Use the built in support for the iterator pattern, the foreach key word.
If using a non generic Dictionary, simply use the KeyValuePair type for the different items:
foreach(KeyValuePair item in myDictionary)
{
DoStuffWith(item);
}
The generic version is almost identical, apart from defining the types in the KeyValuePair to be the same as the dictionary:
foreach(KeyValuePair<Tkey, Tvalue> item in myDictionary)
{
DoStuffWith(item);
}
Depends on whether you're after the keys or the values...
From the MSDN Dictionary<(Of <(TKey, TValue>)>) Class description:
// When you use foreach to enumerate dictionary elements,
// the elements are retrieved as KeyValuePair objects.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
// To get the values alone, use the Values property.
Dictionary<string, string>.ValueCollection valueColl =
openWith.Values;
// The elements of the ValueCollection are strongly typed
// with the type that was specified for dictionary values.
Console.WriteLine();
foreach( string s in valueColl )
{
Console.WriteLine("Value = {0}", s);
}
// To get the keys alone, use the Keys property.
Dictionary<string, string>.KeyCollection keyColl =
openWith.Keys;
// The elements of the KeyCollection are strongly typed
// with the type that was specified for dictionary keys.
Console.WriteLine();
foreach( string s in keyColl )
{
Console.WriteLine("Key = {0}", s);
}
If say, you want to iterate over the values collection by default, I believe you can implement IEnumerable<>, Where T is the type of the values object in the dictionary, and "this" is a Dictionary.
public new IEnumerator<T> GetEnumerator()
{
return this.Values.GetEnumerator();
}
I found this method in the documentation for the DictionaryBase class on MSDN:
foreach (DictionaryEntry de in myDictionary)
{
//Do some stuff with de.Value or de.Key
}
This was the only one I was able to get functioning correctly in a class that inherited from the DictionaryBase.
You suggested below to iterate
Dictionary<string,object> myDictionary = new Dictionary<string,object>();
//Populate your dictionary here
foreach (KeyValuePair<string,object> kvp in myDictionary) {
//Do some interesting things;
}
FYI, foreach
doesn't work if the value are of type object.