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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:25:38+00:00 2026-06-06T13:25:38+00:00

If I have: var myObjects = new ConcurrentBag<object>(); And try to remove objects via:

  • 0

If I have:

var myObjects = new ConcurrentBag<object>();

And try to remove objects via:

foreach (var myObject in myObjects.ToArray())
{
    myObjects.TryTake(out myObject);
}

The compiler complains with: “Readonly local variable cannot be used as an assignment target”

Yet, if I add a local reference within the foreach it compiles:

foreach (var myObject in myObjects.ToArray())
{
    var localReference = myObject;
    myObjects.TryTake(out localReference);
}

What exactly is going on here?

  • 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-06T13:25:41+00:00Added an answer on June 6, 2026 at 1:25 pm

    The iteration variable in a foreach (i.e. myObject) cannot be assigned a new value inside the foreach. It is not allowed.

    In the first scenario, the out tries to do this. In the second scenario, you *never try to reassign to myObject, so it is fine.

    To quote from the ECMA specification, 15.8.4, emphasis mine:

    1. The type and identifier of a foreach statement declare the
      iteration variable of the statement.

    2. The iteration variable corresponds to a read-only local
      variable
      with a scope that extends over the embedded statement.

    3. During execution of a foreach statement, the iteration variable
      represents the collection element for which an iteration is
      currently being performed.

    4. A compile-time error occurs if the embedded statement attempts
      to modify the iteration variable (via assignment or the ++ and
      –operators) or pass the iteration variable as a ref or out parameter.

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

Sidebar

Related Questions

In C#, imagine I have the following object: var myObject = new { Val
I have a function defined that way: var myObject = new Object(); function myFunction(someString){
I have some code that adds properties to an object like this: var MyObject
I have an object that looks like this: var MyObject = { prop1 =
Say I have the following code: var album = new MyObject('album'); Assume that when
I have the following code: var json = MyObject .Select(p => new { id
Let's say I have a MyObject instance which is not initialized: var a:MyObject =
Imagine I have var points = new Point[] { new Point(1, 2), new Point(2,
I have this var n = ItemList.Select(s => new { s.Vchr, s.Id, s.Ctr, s.Vendor,
I have this: var date = new DateTime(2009, 12, 5); ... ... and need

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.