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!