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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:45:52+00:00 2026-05-30T03:45:52+00:00

This is an interesting error I’ve come across while implementing IEnumerable on a class.

  • 0

This is an interesting error I’ve come across while implementing IEnumerable on a class. It appears to be similar to an “access to modified closure” issue, but I’m at a loss as to how to fix it.

Here is a simple example that demonstrates the issue:

void Main()
{
    var nodeCollection = new NodeCollection();
    nodeCollection.MyItems = new List<string>() { "a", "b", "c" };

    foreach (var node in nodeCollection)
    {
        node.Dump();
    }
}

public class NodeCollection : IEnumerable<Node>
{
    public List<string> MyItems;

    public IEnumerator<Node> GetEnumerator()
    {
        // This isn't necessary, but it should prove that it's not an "access to modified closure" issue.
        var items = MyItems;
        for (var i = 0; i < 3; i++)
        {
            var node = new Node();

            // I want the node to contains the items in MyItems.
            node.Items = items;

            // Plus an additional item.  Note that I am adding the item to the node, NOT to MyItems.
            node.Items.Add(string.Format("iteration: {0}", i));

            yield return node;
        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

public class Node
{
    public List<string> Items;
}

As you can see from the Dump() statement, I’m running this in LINQPad, but the issue will present itself in any IDE.

When I run the snippet, I get the following output:

Snippet Result

Because I am adding the item to Items in the newly instantiated Node, I would NOT expect the item to be added to MyItems, but this is obviously what is occurring.

It seems that Items in Node is pointing to MyItems in NodeCollection.

Can anyone tell me:

  • Why this is happening?
  • How to make it not happen?
  • 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-30T03:45:54+00:00Added an answer on May 30, 2026 at 3:45 am

    You are creating new nodes each iteration, but then setting the same items instance to the Items property of each node. Then you are adding the iteration string to the items instance stored in the Items collection (which is always the same instance), resulting in each subsequent node having more and more “iteration” entries. If you kept all of the nodes, you’d find that all of them have exactly the same Items value.

    I think the basic misunderstanding here was that you were assuming that setting the Items property of the Node (node.Items = items;) would copy the items list into the node. In fact, all it does is set node.Items to point to the already-existing list that you call items.

    This should give you an idea where you went wrong:

        // This same instance of items is being reused each time
        var items = MyItems;
        for (var i = 0; i < 3; i++)
        {
            var node = new Node();
    
            // I want the node to contains the items in MyItems.
            // Assuming node.Items is a List<String>
            node.Items = new List<String>();
            node.Items.AddRange(items);
            node.Items.Add(string.Format("iteration: {0}", i));
    
            yield return node;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While writing the touchesBegan handler for my view I ran into this interesting error.
I've been playing with SubSonic lately and I've come across an interesting error. Entity
I ran across an interesting issue in some of my humanize_bytes() code. This loop
Found interesting thing while compile the following piece of code: 1 class A {
I'm having an interesting lifecycle event error, imagine a code hierarchy like this: Page
Attached Firefox to fiddler and got following error. What would cause this error? Interesting
While answering another question I bumped into this interesting situation Where WCF is happy
i have been reading this interesting article which is increasing my every growing confusion
there's this interesting problem i can not solve myself. I will be very glad,
I recently came in contact with this interesting problem. You are given a string

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.