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 6373913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:25:23+00:00 2026-05-25T01:25:23+00:00

for example: 1. foreach (var item in myDic) { if (item.value == 42) myDic.remove(item.key);

  • 0

for example:

1.

         foreach (var item in myDic)
                {
                  if (item.value == 42)
                        myDic.remove(item.key);
                }

would the iterator works properly no matter how the statements in the inner brackets could possibly affect myDic?

2.

    var newDic = myDic.where(x=>x.value!=42).ToDictionary(x=>x.key,x=>x.value);

Is 2nd approach a good practice? functional programming and immutable?

  • 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-25T01:25:24+00:00Added an answer on May 25, 2026 at 1:25 am

    The first approach will crash at runtime, since the enumerator makes sure that nobody deletes from the underlying collection while it’s enumerating.

    The second approach is a nice thought, but C# dictionaries are mutable and it’s neither idiomatic nor efficient to copy them around if you can accomplish the same thing with mutation.

    This is a typical way:

    var itemsToRemove = myDic.Where(f => f.Value == 42).ToArray();
    foreach (var item in itemsToRemove)
        myDic.Remove(item.Key);
    

    EDIT: In response to your question in the comments. Here’s how the example in your other question works:

    myList = myList.where(x=>x>10).select(x=>x-10);
    

    This line of code doesn’t run anything; it’s totally lazy. Let’s say for the sake of argument that we have a foreach after it to make it look more like this question’s example.

    foreach (int n in myList)
        Console.WriteLine(n);
    

    When that executes, here’s what’ll happen on each iteration:

    1. Call MoveNext on the enumerator
    2. The enumerator finds the next value greater than ten
    3. Then it takes that value minus ten and sets the Current property to that
    4. Binds the Current property to the variable n
    5. Console.WriteLines it

    You can see that there’s no mystery and no infinite loop and no whatever.

    Now compare to my example, supposing we left out the ToArray.

    var itemsToRemove = myDic.Where(f => f.Value == 42);
    foreach (var item in itemsToRemove)
        myDic.Remove(item.Key);
    
    1. Call MoveNext on the enumerator
    2. The enumerator finds the next pair with value 42 and sets the Current property to that
    3. Binds the Current property to the variable item
    4. Removes it

    This doesn’t work because while it’s perfectly fine to WriteLine something from a collection while you have an enumerator open on it, you aren’t permitted to Remove something from a collection while you have an enumerator open on it.

    If you call ToArray up front, then you start out by enumerating over the dictionary and populating the array. When we get to the foreach, the foreach statement has an enumerator open on the array, not the dictionary. You’re allowed to remove from the dictionary as you iterate over the array.

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

Sidebar

Related Questions

For example var query = myDic.Where(x => !blacklist.Contains(x.Key)); foreach (var item in query) {
for example foreach (var item in List<int> a) { b.add(item); c+=item; dosomething(); } how
foreach($arrayOne as $value){ do function } In the above example, I'd like to pass
$xml = file_get_contents(example.com); $dom = new DomDocument(); $dom->loadXML($xml); $items = $dom->documentElement; foreach($items->childNodes as $item)
Is there a reason why I can't do the following: foreach (var Item in
I have a loop <ul id=news-list class=thumbobs-list> @foreach (var item in Model.News) { @Html.Partial(RenderNews/
For Example You know foreach loop is heavy And if we use for loop
Can someone share a simple example of using the foreach keyword with custom objects?
example: a_list = [1, 2, 3] a_list.len() # doesn't work len(a_list) # works Python
example: I want to see if array[5] holds a value or is empty.

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.