The statement -
Object x = 10
has a special name and it is called Boxing. That is wrapping a value type inside a reference type. You might be thinking that this should create a reference. Yes you are right. But this also means a new object is created - as mentioned in the doc -
When a value type is boxed, a new object must be allocated and constructed.
https://msdn.microsoft.com/en-us/library/yz2be5wk.aspx
Now when you do -
object y = x;
Y and X are same object here pointing to same memory location. This far is okay. But the next statement -
x = 20;
is causing the exception. You see, this statement is another boxing and as boxing creates new instances, this is creating another new object with value 20 and putting/referring that with X.
So, Y is pointing to the last memory location where X is pointing to a new memory location.
You can understand why by visiting the link I mentioned above.
And if you convince me of the reason, how could I change the object that is referenced by both x and y? => You can't! Because C# does not support explicit pointers and you are using value types.
However if X and Y were reference types (i.e. Class
objects) then you could have done that.