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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:06:08+00:00 2026-05-23T21:06:08+00:00

I understand things in here are value types and not referenced so the field

  • 0

I understand things in here are value types and not referenced so the field _num won’t be modified when I just update the list. But my question is how to update the field _num when I modify the list that contains it gets modified?

class Foo
{
    public List<object> mylist;

    private int _num;

    public int num
    {
        get
        {
            return _num;
        }
        set
        {
            this._num = value;
            mylist[0] = value;
        }

    }

    public Foo()
    {
        mylist = new List<object>();
        mylist.Add(_num);
    }
}


class Program
{
    static void Main(string[] args)
    {
        Foo my = new Foo();
        my.num = 12;
        my.mylist[0] = 5;
        Console.WriteLine("" + my.mylist[0] + " " + my.num);    ==> output is "5 12"
        Console.ReadLine();
    }
}

What changes could be done so the list and the field is synced? Like my output should be “5 5”
Thanks for the help!

  • 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-23T21:06:09+00:00Added an answer on May 23, 2026 at 9:06 pm

    This may or may not be what you want… and I’m still not sure I see the need for modifying the fields by index, but if you really want to do that have you considered an indexer for your type? That is, the indexer would replace your list like so:

    class Foo
    {
        public int num;
        public string name;
        public bool isIt;
    
        public object this[int index]
        {
            get
            {
                switch(index)
                {
                    case 0:
                        return num;
                    case 1:
                        return name;
                    case 2:
                        return isIt;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
            set
            {
                switch(index)
                {
                    case 0:
                        num = (int) value;
                        break;
                    case 1:
                        name = (string) value;
                        break;
                    case 2:
                        isIt = (bool) value;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
        }
    }
    

    Then you can either say:

    var foo = new Foo();
    foo.num = 13;  // either works
    foo[0] = 13;  // either works
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not completely sure I understand the workflow way of doing things, but if
I am not able to understand few things on the Garbage collection. Firstly, how
I've read a couple of things about MVCs but I still don't understand when
I am new to Windbg and trying to understand a few things about value
I'm struggling with core data... I don't understand some things so if someone could
I don't truly understand some basic things in C like dynamically allocating array of
One of the most difficult things about understand Spring is that Spring supports multiple
There are things in ARM elf binaries I'd like to understand better. I need
I try to understand the memory management in ObjectiveC and still some things are
What are the things to consider when choosing between ByRef and ByVal. I understand

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.