[Solved] Why does assignment to parameter not change object?


The observed behavior has nothing to do with Value types vs Reference types – it has to do with the Evaluation of Strategy (or “calling conventions”) when invoking a method.

Without ref/out, C# is always Call by Value1, which means re-assignments to parameters do not affect the caller bindings. As such, the re-assignment to the x parameter is independent of the argument value (or source of such value) – it doesn’t matter if it’s a Value type or a Reference type.

See Reference type still needs pass by ref? (on why caller does not see parameter re-assignment):

Everything is passed by value in C#. However, when you pass a reference type, the reference itself is being passed by value, i.e., a copy of the original reference is passed. So, you can change the state of object that the reference copy points to, but if you assign a new value to the reference [parameter] you are only changing what the [local variable] copy points to, not the original reference [in the argument expression].

And Passing reference type in C# (on why ref is not needed to mutate Reference types)

I.e. the address of the object is passed by value, but the address to the object and the object is the same. So when you call your method, the VM copies the reference; you’re just changing a copy.


1 For references types, the phrasing “Call By Value [of the Reference]” or “Call by [Reference] Value” may help clear up the issue. Eric Lippert has written a popular article The Truth about Value Types which encourages treating reference values as a distinct concept from References (or instances of Reference types).

1

solved Why does assignment to parameter not change object?