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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:29:17+00:00 2026-06-05T10:29:17+00:00

It is known that while iterating some IEnumerable in C#, no modifications can be

  • 0

It is known that while iterating some IEnumerable in C#, no modifications can be made to elements of the enumerable collection:

// Illegal code
foreach (Employee e in employeeList)
{
     e.Salary = 1000000;
}

I am wondering how is this being enforced by the runtime or by the enumerator itself?

  • 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-06-05T10:29:19+00:00Added an answer on June 5, 2026 at 10:29 am

    I think you’re misunderstanding the restriction. The code you posted is perfectly legal, and in fact, if it were not then the foreach loop would be pretty useless.

    What is not permitted is changing the sequence while in the middle of a foreach loop:

    foreach ( Employee e in employeeList )
    {
      if ( e.Salary > 10000000 ) 
      {
        employeeList.Remove(e);
      }
    }
    

    This code throw an InvalidOperationException at run-time: the call to MoveNext in the IEnumerator implementation will throw when it detects that the underlying collection has changed between calls. This is a requirement of implementing the enumerator object — each implementation of IEnumerable has to deal with this possibility for itself. Note that certain collections (the new concurrent ones from .NET 4.0) explicitly do not enforce this restrictions, and thus the code above would be legal of employeeList was, for example, a ConcurrentDictionary. See this blog post for details.

    It is also not legal to assign a value to the loop control variable, e.g.:

    foreach ( Employee e in employeeList )
    {
      if ( e.Salary > 10000000 ) 
      {
        e = new Employee { Salary = 999999 };
      }
    }
    

    The reason this isn’t allowed is because it doesn’t make much sense and is almost certainly a bug; see this answer for details: Why is The Iteration Variable in a C# foreach statement read-only?

    Note that this isn’t attempting to change the elements in the sequence — its merely attempting to change the value of the local variable used as the loop control variable. Also, this isn’t enforced by the enumerator nor the run-time. It’s a compile-time error, and is enforced by the C# compiler.

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

Sidebar

Related Questions

I know that I can't modify a collection while iterating over it. Normally I
I know that it is not allowed to remove elements while iterating a list,
Now while I know that you can not perform inheritance like you would in
I known that with subversion you can attach arbitrary properties to each files. In
I have an activity that gets some data from the internet in background while
It is known that all functional languages share some basic properties like using functions
I often accidently step into code that I'm not interested in while debugging in
hi all i want to know that while specifying url in my iphone native
I have played around with Scala for a while now, and I know that
Also I want to know how to add meta data while indexing so that

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.