Boxed links should be unchanged. For example, this will not compile:
((Point)p).X += 3;
As others have said, this line causes a couple of packing and unpacking operations, which ends with a new link:
age2 = (int)age2 + 3;
Thus, even though the packed int is actually a link, the line above also changes the link to the object, so the caller will still see the same content if the object itself is not passed by reference.
However, there are several ways to dereference and change boxed values ββwithout changing the link (however, none of them are recommended).
Solution 1:
The easiest way is through reflection. This seems a little silly because the Int32.m_value field is the int value itself, but it allows you to access the int directly.
private static void AddThree(object age2) { FieldInfo intValue = typeof(int).GetTypeInfo().GetDeclaredField("m_value"); intValue.SetValue(age2, (int)age2 + 3); }
Solution 2:
This is a much more TypedReference hack and involves using the mostly undocumented TypedReference and the __makeref() operator, but more or less this is what happens in the background in the first solution:
private static unsafe void AddThree(object age2) {
source share