i tried:
int i = 5;
object o1 = i; // boxing the i into object (so it should be a reference type)
object o2 = o1; // set object reference o2 to o1 (so o1 and o2 point to same place at the heap)
o2 = 8; // put 8 to at the place at the heap where o2 points
after running this code, value in o1 is still 5, but i expected 8.
Am I missing something?
That’s not how variables in C# work. It has nothing to do with boxing value types.
Consider this:
Why would you expect
o1ando2to contain a reference to the same object? They are the same when you seto2 = o1, but once you seto2 = new object(), the value (the memory location pointed to by the variable) ofo2changes.Maybe what you’re trying to do can be done like this:
At the end of
Main, theValproperty ofo1will contain8.