When iterating over a dictionary in C#, are we guaranteed that the order will be same every time we iterate this dictionary?
foreach (var key in myDict.Keys)
{
...
}
When iterating over a dictionary in C#, are we guaranteed that the order will be same every time we iterate this dictionary?
foreach (var key in myDict.Keys)
{
...
}
According to documentation, no, there is no guarantee:
The order of the keys in the
Dictionary<TKey, TValue>.KeyCollection
is unspecified,but it is the same order as the associated values in theDictionary<TKey, TValue>.ValueCollection
returned by the Values property.