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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:53:20+00:00 2026-06-15T06:53:20+00:00

I’ve got a work around for this issue, but I’m trying to figure out

  • 0

I’ve got a work around for this issue, but I’m trying to figure out why it works . Basically, I’m looping through a list of structs using foreach. If I include a LINQ statement that references the current struct before I call a method of the struct, the method is unable to modify the members of the struct. This happens regardless of whether the LINQ statement is even called. I was able to work around this by assigning the value I was looking for to a variable and using that in the LINQ, but I would like to know what is causing this. Here’s an example I created.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WeirdnessExample
{
    public struct RawData
    {
        private int id;

        public int ID
        {
            get{ return id;}
            set { id = value; }
        }

        public void AssignID(int newID)
        {
            id = newID;
        }
    }

    public class ProcessedData
    {
        public int ID { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<ProcessedData> processedRecords = new List<ProcessedData>();
            processedRecords.Add(new ProcessedData()
            {
                ID = 1
            });


            List<RawData> rawRecords = new List<RawData>();
            rawRecords.Add(new RawData()
            {
                ID = 2
            });


            int i = 0;
            foreach (RawData rawRec in rawRecords)
            {
                int id = rawRec.ID;
                if (i < 0 || i > 20)
                {
                    List<ProcessedData> matchingRecs = processedRecords.FindAll(mr => mr.ID == rawRec.ID);
                }

                Console.Write(String.Format("With LINQ: ID Before Assignment = {0}, ", rawRec.ID)); //2
                rawRec.AssignID(id + 8);
                Console.WriteLine(String.Format("ID After Assignment = {0}", rawRec.ID)); //2
                i++;
            }

            rawRecords = new List<RawData>();
            rawRecords.Add(new RawData()
            {
                ID = 2
            });

            i = 0;
            foreach (RawData rawRec in rawRecords)
            {
                int id = rawRec.ID;
                if (i < 0)
                {
                    List<ProcessedData> matchingRecs = processedRecords.FindAll(mr => mr.ID == id);
                }
                Console.Write(String.Format("With LINQ: ID Before Assignment = {0}, ", rawRec.ID)); //2
                rawRec.AssignID(id + 8);
                Console.WriteLine(String.Format("ID After Assignment = {0}", rawRec.ID)); //10
                i++;
            }

            Console.ReadLine();
        }
    }
}
  • 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-15T06:53:21+00:00Added an answer on June 15, 2026 at 6:53 am

    Okay, I’ve managed to reproduce this with a rather simpler test program, as shown below, and I now understand it. Admittedly understanding it doesn’t make me feel any less nauseous, but hey… Explanation after code.

    using System;
    using System.Collections.Generic;
    
    struct MutableStruct
    {
        public int Value { get; set; }
    
        public void AssignValue(int newValue)
        {
            Value = newValue;
        }
    }
    
    class Test
    {
        static void Main()
        {
            var list = new List<MutableStruct>()
            {
                new MutableStruct { Value = 10 }
            };
    
            Console.WriteLine("Without loop variable capture");
            foreach (MutableStruct item in list)
            {
                Console.WriteLine("Before: {0}", item.Value); // 10
                item.AssignValue(30);
                Console.WriteLine("After: {0}", item.Value);  // 30
            }
            // Reset...
            list[0] = new MutableStruct { Value = 10 };
    
            Console.WriteLine("With loop variable capture");
            foreach (MutableStruct item in list)
            {
                Action capture = () => Console.WriteLine(item.Value);
                Console.WriteLine("Before: {0}", item.Value);  // 10
                item.AssignValue(30);
                Console.WriteLine("After: {0}", item.Value);   // Still 10!
            }
        }
    }
    

    The difference between the two loops is that in the second one, the loop variable is captured by a lambda expression. The second loop is effectively turned into something like this:

    // Nested class, would actually have an unspeakable name
    class CaptureHelper
    {
        public MutableStruct item;
    
        public void Execute()
        {
            Console.WriteLine(item.Value);
        }
    }
    
    ...
    // Second loop in main method
    foreach (MutableStruct item in list)
    {
        CaptureHelper helper = new CaptureHelper();
        helper.item = item;
        Action capture = helper.Execute;
    
        MutableStruct tmp = helper.item;
        Console.WriteLine("Before: {0}", tmp.Value);
    
        tmp = helper.item;
        tmp.AssignValue(30);
    
        tmp = helper.item;
        Console.WriteLine("After: {0}", tmp.Value);
    }
    

    Now of course each time we copy the variable out of helper we get a fresh copy of the struct. This should normally be fine – the iteration variable is read-only, so we’d expect it not to change. However, you have a method which changes the contents of the struct, causing the unexpected behaviour.

    Note that if you tried to change the property, you’d get a compile-time error:

    Test.cs(37,13): error CS1654: Cannot modify members of 'item' because it is a
        'foreach iteration variable'
    

    Lessons:

    • Mutable structs are evil
    • Structs which are mutated by methods are doubly evil
    • Mutating a struct via a method call on an iteration variable which has been captured is triply evil to the extent of breakage

    It’s not 100% clear to me whether the C# compiler is behaving as per the spec here. I suspect it is. Even if it’s not, I wouldn’t want to suggest the team should put any effort into fixing it. Code like this is just begging to be broken in subtle ways.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

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.