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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:26:19+00:00 2026-05-27T04:26:19+00:00

Why do I have to create a concrete implementation of IEnumerable<T> in order to

  • 0

Why do I have to create a concrete implementation of IEnumerable<T> in order to modify its members in the foreach loop?

This blog post (Exhibit 1) explains the behavior, but I can’t quite wrap my head around it.

I’ve got a very simple code snippet here to reproduce the issue (C# 4.0 / .NET 4.0).

class Person
{
    public int Age { get; set; }

    public Person()
    {

    }
}

class Program
{
    static void Main(string[] args)
    {
        //calling .ToList() on GetPeople() below will fix the issue
        var people = GetPeople();

        foreach (var item in people)
        {
            item.Age = DateTime.Now.Second;
        }

        foreach (var item in people)
        {
            Console.WriteLine("Age is {0}", item.Age);
        }

        Console.Read();
    }

    public static IEnumerable<Person> GetPeople()
    {
        int i = 0;
        while (i < 3)
        {
            i++;
            yield return new Person(); 
        }
    }
}
  • 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-27T04:26:20+00:00Added an answer on May 27, 2026 at 4:26 am

    Each time you iterate over people, it will execute the code in GetPeople() again – creating new instances of Person. The code in GetPeople does not run when you call GetPeople(); it only starts running when you call something:

    var iterator = people.GetEnumerator();
    iterator.MoveNext();
    

    … which is what the foreach loop does.

    If you call ToList(), that means you only execute the code in GetPeople() once, and store the references returned when iterating over the sequence. At that point, each time you iterator over the List<Person> you’ll iterate over references to the same objects, so any modifications you make in one loop will be seen in another.

    You may find it slightly easier to understand what’s going on if you put logging (or a breakpoint) in GetPeople(). I have an article which goes into the implementation details, which may make things clearer too.

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

Sidebar

Related Questions

I have create name range on sheet A so I need to use this
I have create my own NSOpenGLView class, right now the data that i want
i have create a form (so it's PHP and HTML hybrid-code). it has ability
I have create a listfragment and a button which looks like as follows (
I have create web app in MVS 3 but failed to display alert message
I have create a custom contextMenu using AS3 and can apply that to the
I have create a widget in android , whenever i click on Widget TextView
I have create a listview of Contact numbers , the list item contains the
I have create an array containing person firstname,lastname and number which i populate in
I have create the the jqgrid which loads the data from the server ,

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.