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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:17:50+00:00 2026-05-15T16:17:50+00:00

Why I am not able to modify List items? struct Foo { public string

  • 0

Why I am not able to modify List items?

struct Foo 
{
    public string Name;
}

Foo foo = new Foo();
foo.Name = "fooNameOne";

List<Foo> foos = new List<Foo>();
foos.Add(foo);

// Cannot modify the return value of 
// 'List<Foo>.this[int]' because it is not a variable   
//foos[0].Name = "fooNameTwo";

Foo tempFoo = foos[0];
tempFoo.Name = "fooNameTwo";

Console.WriteLine(foos[0].Name); // fooNameOne

EDIT:
I want to leave the structure for Foo. How should I proceed? foos[0] = tempFoo? little bit complicated just for an assignment?!.

  • 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-15T16:17:51+00:00Added an answer on May 15, 2026 at 4:17 pm

    Here’s the thing.

    You say this:

    I want to leave the structure for Foo.
    How should I proceed? foos[0] =
    tempFoo? little bit complicated just
    for an assignment?!.

    Yeah, well, that’s just it. What you need is an assignment, not a modification. Value types (structs) are generally supposed to be treated as values.

    Take int. If you had a List<int> called ints, how would you change the value at ints[0]?

    Something like this, right?

    ints[0] = 5; // assignment
    

    Notice there’s no way to do something like this:

    ints[0].ChangeTo(5); // modification?
    

    That’s because Int32 is an immutable struct. It is intended to be treated as a value, which cannot be changed (so an int variable can only be assigned to something new).

    Your Foo struct is a confusing case because it can be mutated. But since it’s a value type, only copies are passed around (same as with int, double, DateTime, etc. — all the value types we deal with on a regular basis). For this reason you cannot mutate an instance "from afar"–that is, unless it is passed to you by ref (using the ref keyword in a method call).

    So the simple answer is, yeah, to change a Foo in a List<Foo>, you need to assign it to something new. But you really shouldn’t have a mutable struct to begin with.

    Disclaimer: As with almost any advice you can receive, in anything, this is not a 100% hard rule. The very skilled developer Rico Mariani wrote the Point3d struct to be mutable for good reasons, which he explained on his blog. But this is a case of a very knowledgeable developer knowing exactly what he’s doing; in general, as a standard approach to writing value types versus reference types, value types should be made immutable.


    In response to your comment: so when you are dealing with a mutable struct like Point, basically you need to do something like this:

    Point p = points[0];
    p.Offset(0, 5);
    points[0] = p;
    

    Or, alternatively:

    Point p = points[0];
    points[0] = new Point(p.X, p.Y + 5);
    

    The reason I wouldn’t do…

    points[0] = new Point(points[0].X, points[0].Y + 5);
    

    …is that here you’re copying the value at points[0] twice. Remember that accessing the this property by index is basically a method call. So that code is really doing this:

    points.set_Item(0, new Point(points.get_Item(0).X, points.get_Item(0).Y + 5);
    

    Note the excessive call to get_Item (making an additional copy for no good reason).

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

Sidebar

Ask A Question

Stats

  • Questions 485k
  • Answers 486k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's entirely feasible. Bots aren't very smart, and they can… May 16, 2026 at 7:52 am
  • Editorial Team
    Editorial Team added an answer This is generally not an issue, but I would recommend… May 16, 2026 at 7:52 am
  • Editorial Team
    Editorial Team added an answer Check out the Secure Pages module: http://drupal.org/project/securepages May 16, 2026 at 7:52 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.