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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:59:50+00:00 2026-05-18T10:59:50+00:00

I have a simple class which has boolean field: public struct Foo { bool

  • 0

I have a simple class which has boolean field:

public struct Foo { bool isAvailable; }

Now I have a List of foos:

List < Foo >  list = new List< Foo >();

Later on I enumerate each foo in the list and try to update its isAvailable field:

foreach(Foo foo in list) {
    foo.isAvailable = true; 
}

But the above code never updates the list. What am I doing wrong here and what’s its remedy.

  • 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-18T10:59:51+00:00Added an answer on May 18, 2026 at 10:59 am

    It’s because Foo is a mutable struct.

    When you fetch the value from the list, it’s making a copy – because that’s how value types behave. You’re changing the copy, leaving the original value unchanged.

    Suggestions:

    • You probably should be using a class
    • Don’t create mutable structs. They behave in ways which can be hard to predict, or at least not the way you might expect when you’re not explicitly thinking about it.

    While you could change your code to iterate over the list in a different way and replace the value each time, it’s generally a bad idea to do so. Just use a class… or project your list to a new list with the appropriate values.


    Original answer, when Foo was a class

    It should work fine. For example, here’s a short but complete program which does work:

    using System.Collections.Generic;
    
    public class Foo
    {
        public bool IsAvailable { get; set; }
        public string Name { get; set; }
    
        public override string ToString()
        {
            return Name + ": " + IsAvailable;
        }
    }
    
    class Test
    {
        static void Main()
        {
            List<Foo> list = new List<Foo>()
            {
                new Foo { Name = "First", IsAvailable = true },
                new Foo { Name = "Second", IsAvailable = false },
                new Foo { Name = "Third", IsAvailable = false },
            };
    
            Console.WriteLine("Before:");
            list.ForEach(Console.WriteLine);
            Console.WriteLine();
    
            foreach (Foo foo in list)
            {
                foo.IsAvailable = true;
            }
    
            Console.WriteLine("After:");
            list.ForEach(Console.WriteLine);
        }
    }
    

    Try to adapt your current code to a similar short but complete program which doesn’t work, post that, and we can work out what’s going on.

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

Sidebar

Related Questions

I have a simple matrix class which has a 2d integer pointer field in
i have created a simple public ref class in the vc++ project, which is
I have simple ontology called campus.owl.There is a class calledLecturer and which has two
I have an class which has a enum property and a boolean property, based
I have a simple class which has a static constructor and a instance constructor.
I have a relatively simple class which deletes a post: function delete_post($postid, $reason){ //Stuff
I have simple class with width and height member fields which define number of
I have a simple Java class in which I invoke a call to a
I have a simple class for use on JNI, which i need to export
I have a simple model class (Part), which pulls from it's information from a

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.