Iterate Dictionary Using C#

Dictionary is a object that can store values in Key-Value pair. its just like a list, the only difference is:
List can be iterate using index(0-n) but not the Dictionary.
Generally when we try to iterate the dictionary we get below error:

Collection was modified; enumeration operation may not execute.

So How to parse a dictionary and modify its values??
To iterate dictionary we must loop through it’s keys or key – value pair.

<pre>            //get key collection from dictionary into a list to loop through  	
            List&lt;int&gt; keys = new List&lt;int&gt;(Dictionary.Keys);
            // iterating key collection using simple for-each loop
            foreach (int key in keys)
            	// Now we can perform any modification with values of dictionary.    
            	Dictionary[key] = Dictionary[key] - 1; 

Say something : I accept all the "Humer&Critic"

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s