foreach(KeyValuePair<string, string> entry in myDictionary)
{
    // do something with entry.Value or entry.Key
}如果您尝试在 C#中使用通用字典,则可以使用另一种语言的关联数组:
foreach(var item in myDictionary)
{
  foo(item.Key);
  bar(item.Value);
}或者,如果您只需要遍历密钥集合,请使用
foreach(var item in myDictionary.Keys)
{
  foo(item);
}最后,如果你只对价值感兴趣:
foreach(var item in myDictionary.Values)
{
  foo(item);
} (请注意, var关键字是可选的 C#3.0 及以上功能,您也可以在此处使用键 / 值的确切类型) 
在某些情况下,您可能需要一个可以通过 for 循环实现提供的计数器。为此,LINQ 提供了ElementAt ,它支持以下功能: 
for (int index = 0; index < dictionary.Count; index++) {
  var item = dictionary.ElementAt(index);
  var itemKey = item.Key;
  var itemValue = item.Value;
}