Alex said:
Maybe I don't.
Would you be so kind to explain the difference between the following statements:
1) Object X is passed to function F by reference.
2) A reference to Object X is passed to function F by value.
Sure. Changing the value of the actual parameter in the function F has
no effect (as far as the caller sees) in the second case, but changes
the value of the formal parameter in the first case.
I claim that the statements are equivalent, as they both describe
putting a reference to object X on the stack (or a register,
whatever) before calling function F. The difference is semantic but
it can help visualize what is going on.
If you think that I am mistaken, please show me how.
See above.
Your article, as I understand it, talks about two different types of
objects - a value object that is passed by reference and a reference
object that is passed by value. In that case the behaviour that you
are trying to show is indeed different but that is something
different altogether.
While there are value types and reference types in .NET, it doesn't
really alter anything. If you pass something by value, then changing
the value which is passed doesn't affect the caller's value. If you
pass something by reference, the variable used by the called method is
the same variable (or l-value, whatever) as the caller is using -
changing one changes the other.
My mistake. I was using generic English terms and not specific .NET terminology.
If I replace "object" with "entity" will you agree with my statement then?
Nope, because it's the "pass by reference" which has a very specific
meaning. By claiming that objects are passed by reference, you're
implying semantics which just don't occur in .NET.
Actually, passing a pointer in C is equivalent to "pass by reference".
No it's not - that's the point. It's not the same thing in the strict
sense of the terminology, which means that if you tell people who *do*
know the strict sense of the terminology that objects are passed by
reference, they'll expect behaviour other than what they get.
Many of the uses of pass by reference can be simulated by passing an
address/reference/pointer/whatever by value, but it's not the same as
pass by reference in itself.
And the actual behaviour is pretty close to your example:
Passing a pointer to a data type (I'm being careful not to say
"object") allows you to change the value if that data type from
inside the function, but *not* to "re-seat" the pointer (make it
point to a different instance of that data type.
Changing the value of the pointer (the parameter) doesn't change the
caller's value though - it's only changing the value of what the
pointer points at that makes the change visible. The parameter here is
the pointer, not the value of the variable whose address is passed as a
pointer.
The same principle holds with C++ references.
I don't like to use C++ terminology as I don't know it well enough to
be confident of being strictly correct.
C# (and, if I recall correctly, Java) confuses matters a bit by only
allowing "reference types" to reside on the heap and "value types" on
the stack but there is no fundamental difference in behaviour.
Even that's not true, as value types also reside on the heap within
reference type objects. Anyway...
See
http://www.yoda.arachsys.com/java/passing.html for another way of
putting things.