In the ModifySerialized
method, you're setting the target
parameter to a new instance of DataClass
; however, since the parameter is not passed by reference, the testDataClass
variable in the Test
method still refers to the original instance, which has never been modified. Passing the parameter by reference would produce the behavior you expect.
What am I missing? The parameter to the function is a reference, no? The value of the reference should be modified, no? In contrast, if inside ModifySerialized(target) I just set Number=1234, the value would be correctly output to console after the call from Test as expected.
I think you're confusing two concepts here:
- value types vs. reference types
- passing parameters by value vs. by reference
Here, the type of the parameter is a reference type, which means that the value of the parameter is a reference; but the reference is passed by value, so assigning a new instance to the parameter doesn't change the original reference.
For more detailed explanations, I suggest you read Jon Skeet's excellent article: Parameter passing in C#