William Stacey said:
Thanks Jon, instead of commenting on all that, here is a document I created
that gets into the detail of this at the memory level. This shows exactly
what gets passed when passing a byte[] by-value and by-reference. There is
also a few other bits that you may find interesting or could answer
(question is noted in the text.) Please feel free to comment. If you can't
view this, please also let me know. Cheers!
http://www.mvptools.com/docs/PassByteArrays.mht
The problem with looking at it at the implementation level is that
pass-by-reference and pass-by-value aren't *defined* at the
implementation level. They're defined in terms of parameters. When you
talk about the "pointers [to the variables themselves] being passed by
value" in paragraph 3, that doesn't apply to any definition of pass-by-
value I've seen, because the pointers aren't the value of any of the
parameters. The point of describing pass-by-reference and pass-by-value
semantics is to understand how *parameters* are passed.
In the case of pass-by-value, the value of the actual parameter
expression becomes the value of the new local variable (the formal
parameter).
In the case of pass-by-reference, the formal parameter is aliased with
the actual parameter expression (which must therefore be an L-value) so
that any changes in the formal parameter's value are also visible as
changes to the actual parameter expression's value.
The parameters in the call
GetRefs (ba, ba2, ref ba, ref ba2)
are ba, ba2, ba (again), and ba2 (again). The first parameters are
passed by value, the second are passed by reference. There are no other
parameters, so no other things can really be described as being passed,
IMO. The title of your page is misleading in this respect - byte arrays
themselves can never be passed, as there is no expression whose value
is a byte array - only a *reference* to a byte array.
One of the reasons the explanation gets difficult (paragraph 6) is that
the JIT gets involved - and frankly, I don't understand the details of
what the JIT does. That's the good thing about sticking at the language
level - everything can be explained by the language specification.
Obviously we need to have some ideas about performance etc, but in
terms of explaining the way the language behaves, we can stick at the
language specification level.