What is the best way to iterate over a dictionary?

前端 未结 30 2408
我寻月下人不归
我寻月下人不归 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:51

    Sometimes if you only needs the values to be enumerated, use the dictionary's value collection:

    foreach(var value in dictionary.Values)
    {
        // do something with entry.Value only
    }
    

    Reported by this post which states it is the fastest method: http://alexpinsker.blogspot.hk/2010/02/c-fastest-way-to-iterate-over.html

提交回复
热议问题