What is the best way to iterate over a dictionary?

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

    There are plenty of options. My personal favorite is by KeyValuePair

    Dictionary myDictionary = new Dictionary();
    // Populate your dictionary here
    
    foreach (KeyValuePair kvp in myDictionary)
    {
         // Do some interesting things
    }
    

    You can also use the Keys and Values Collections

提交回复
热议问题