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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:44:54+00:00 2026-06-10T05:44:54+00:00

The code is: struct m1 { public int a; public int b; } int

  • 0

The code is:

struct m1
{
    public int a;
    public int b;
}

int main()
{
    List <m1> mList;
    m1.initialize;
    //new. and add some items to it.

now I want to access the objects in mList
at first I tried:

    for each(m1 it in mList)
    {
        m1.a = 5;
    }

but it failed. becuase after the for each I wrote m1.first().a on console. it was it’s initialized value not 5.

then I tried

    for (int counter = 0; counter < mList.size(); counter++)
    {
        m1 it = mList[counter];
        it.a = 5;
    }

again the same problem.

then I tried

    for (int counter = 0; counter < mList.size(); counter++)
    {
        mList[counter].a = 5;
    }

it even didn’t compiled. it gives me an error. it says something about not being modifiable return value of list.this[int].

then I tried

    for (int counter = 0; counter < mList.size(); counter++)
    {
        var m1 it = mList[counter];
        it.a = 5;
    }

it didn’t work too.
I tried all I could and everything that I found in internet and this site.
Could you please help you to find a way to access parameters of objects(of type struct) in list?
Obviously it is easy when list is made from objects(from classes). it comes to complication if I want to made a list from objects of structs.
Any help would highly welcomed.

  • 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-10T05:44:56+00:00Added an answer on June 10, 2026 at 5:44 am

    The problem is that you’ve put a struct in a list. When you fetch it from the list, that will create a copy – so modifying it won’t do any good. Given your comments, it’s possible that you’re confused about how value types and reference types work in C#, in which case I suggest you read my article on the topic (or books, or MSDN etc).

    You’d need to fetch it, modify the copy, then replace the value in the list:

    var copy = list[index];
    copy.a = 5;
    list[index] = copy;
    

    However, I would strongly advise against creating mutable value types in the first place, partly because they cause exactly this sort of problem. Value types should usually be used for more “fundamental” values which can’t be changed in place. It doesn’t make sense to change what “the number 5” means, for example, or “January 1st at 10am”. If you change the meaning, you’ve got a new value – so force that to genuinely be a new value.

    It’s unclear (due to lack of context) whether you should be creating an immutable value type and replacing it in the list, or creating a mutable reference type (a class) instead. Or possibly even an immutable reference type, as that can often aid readability.

    I’d also advise against using public fields – use properties instead, to separate implementation details from the type’s API.

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

Sidebar

Related Questions

I have some code to initialize a struct in C#: namespace Practice { public
Code to illustrate : public struct MyStruct { public int SomeNumber; } public string
Having code: struct B { int* a; B(int value):a(new int(value)) { } B():a(nullptr){} B(const
Code: struct A{ const bool const_some_write_once_flag; A(): const_some_write_once_flag(false) { } }; struct B: public
I have this code (C#): using System.Collections.Generic; namespace ConsoleApplication1 { public struct Thing {
In the following code: struct Foo { Foo(int x=0); }; Does the constructor count
struct mydata { public int id; public string data; } class Program { static
Why is it that in the code below template<typename T> struct Child : public
struct A { protected: int y; public: int z; }; struct F : A
If I have a code like this: struct A { virtual void f(int) {}

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.