Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6067193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:34:55+00:00 2026-05-23T09:34:55+00:00

I use this: foreach(KeyValuePair<String,String> entry in MyDic) { // do something with entry.Value or

  • 0

I use this:

foreach(KeyValuePair<String,String> entry in MyDic)
  {
      // do something with entry.Value or entry.Key

  }

The problem is that I can’t change the value of entry.Value or entry.Key

My question is that how can i change the value or key when looping through a dictionary?
And, does dictionary allow duplicated key? And if yes, how can we avoid ?
Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:34:56+00:00Added an answer on May 23, 2026 at 9:34 am

    You cannot change the value of a dictionary entry while looping through the items in the dictionary, although you can modify a property on the value if it’s an instance of a reference type.

    For example,

    public class MyClass 
    {
        public int SomeNumber { get; set;}
    }
    
    foreach(KeyValuePair<string, MyClass> entry in myDict)
    {
        entry.Value.SomeNumber = 3; // is okay
        myDict[entry.Key] = new MyClass(); // is not okay
    }
    

    Trying to modify a dictionary (or any collection) while looping through its elements will result in an InvalidOperationException saying the collection was modified.

    To answer your specific questions,

    My question is that how can i change the value or key when looping through a dictionary?

    The approach to both will be pretty much the same. You can either loop over a copy of the dictionary as Anthony Pengram said in his answer, or you can loop once through all the items to figure out which ones you need to modify and then loop again through a list of those items:

    List<string> keysToChange = new List<string>();
    foreach(KeyValuePair<string, string> entry in myDict)
    {
        if(...) // some check to see if it's an item you want to act on
        {
            keysToChange.Add(entry.Key);
        }
    }
    
    foreach(string key in keysToChange)
    {
       myDict[key] = "new value";
    
       // or "rename" a key
       myDict["new key"] = myDict[key];
       myDict.Remove(key);
    }
    

    And, does dictionary allow duplicated key? And if yes, how can we avoid ?

    A dictionary does not allow duplicate keys. If you want a collection of <string, string> pairs that does, check out NameValueCollection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So for example: ConcurrentDictionary<string,Payload> itemCache = GetItems(); foreach(KeyValuePair<string,Payload> kvPair in itemCache) { if(TestItemExpiry(kvPair.Value)) {
I know in C# you use foreach like this: foreach string str in array1
How can I edit this foreach loop so that I will be able to
I have this code: NameValueCollection nv = HttpUtility.ParseQueryString(queryString); foreach (KeyValuePair<String,String> pr in nv) {
i have this crazy label for each time i create a migration that use
I use this code to update data in database table. Can reuse same code
I use this code to process a date string coming in from a json
Which of these patterns should be preferred over the other and why? foreach(KeyValuePair<string, Dictionary<string,
I have a dictionary, where the key is a string and the value is
so I want to use this JQuery plugin that Stack Overflow has made available

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.