How to update a field of a class when a list of the class gets modified in C#? -


i understand things in here value types , not referenced field _num won't modified when update list. question how update field _num when modify list contains gets modified?

class foo {     public list<object> mylist;      private int _num;      public int num     {                 {             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 = new foo();         my.num = 12;         my.mylist[0] = 5;         console.writeline("" + my.mylist[0] + " " + my.num);    ==> output "5 12"         console.readline();     } } 

what changes done list , field synced? output should "5 5" help!

this may or may not want... , i'm still not sure see need modifying fields index, if want have considered indexer type? is, indexer replace list so:

class foo {     public int num;     public string name;     public bool isit;      public object this[int index]     {                 {             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 can either say:

var foo = new foo(); foo.num = 13;  // either works foo[0] = 13;  // either works 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -