What is the best way to iterate over a dictionary?

前端 未结 30 2175
我寻月下人不归
我寻月下人不归 2020-11-22 05:18

I\'ve seen a few different ways to iterate over a dictionary in C#. Is there a standard way?

30条回答
  •  天涯浪人
    2020-11-22 05:38

    In some cases you may need a counter that may be provided by for-loop implementation. For that, LINQ provides ElementAt which enables the following:

    for (int index = 0; index < dictionary.Count; index++) {
      var item = dictionary.ElementAt(index);
      var itemKey = item.Key;
      var itemValue = item.Value;
    }
    

提交回复
热议问题