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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:32:58+00:00 2026-06-06T07:32:58+00:00

I have list of structure. I want to modify a particular data from the

  • 0

I have list of structure. I want to modify a particular data from the structure.
And the structure is at the particular index location of the List.

I want to do something like this:

struct sample
{
  int a;
  string name;
}

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


for(int i=0; i < list.Count; i++)
{
  list[i].a = someotherlist[i].data;
}
  • 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-06T07:33:00+00:00Added an answer on June 6, 2026 at 7:33 am

    The problem is that the list indexer creates a copy of the struct, i.e. it is really:

    for(int i=0; i < list.Count; i++)
    {
      var completelyIsolatedClone = list[i];
      completelyIsolatedClone.a = someotherlist[i].data;
    }
    

    The compiler is preventing you making an obvious mistake. The code you have uses the get, mutates a separate copy of the data, but never puts it back – so your change doesn’t do anything useful. Note that the Mono folks think it would be nice if it worked your way, though: http://tirania.org/blog/archive/2012/Apr-11.html

    Note that an array works differently; with an array you are touching the struct in place, so the following would work fine:

    for(int i=0; i < list.Count; i++)
    {
      someArray[i].a = someotherlist[i].data;
    }
    

    Another approach is to copy the data out (the get accessor), mutate it, and put it back:

    for(int i=0; i < list.Count; i++)
    {
      var completelyIsolatedClone = list[i];
      completelyIsolatedClone.a = someotherlist[i].data;
      list[i] = completelyIsolatedClone;
    }
    

    or better, acoid mutable structs completely, perhaps with a method that applies the change to a new copy:

    for(int i=0; i < list.Count; i++)
    {
      list[i] = list[i].SetA(someotherlist[i].data);
    }
    

    where SetA creates a new struct, like DateTime.AddDays etc, i.e.

    public SomeType SetA(int a) {
        return new SomeType(this.X, this.Y, a, this.Z);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have a List of an Record(structure) like this : struct Rec { int
I have some C structures related to a 'list' data structure. They look like
I have a structure that roughly looks like this: List<ProductLine> -> ID Name ...
I have a list of data that I want to search through. This new
I have a file structure on my server that looks something like this: My
I have a list structure like this: <ul id=part> <?php $i=0; foreach ($q as
I have Adjacency list mode structure like that and i want to count all
I have a list like this list <- structure(list(`1` = structure(c(274L, 173L), .Dim =
I have a list of all direct flights. From this I want to get

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.