John A. Bailo said:
Well, isn't it just more like a pointer to an object -- or value?
That sounds more like the case where ref *isn't* used. In the part you
quoted I was referring to it being used.
Another thing, they say it's an object reference passed by value to the
method.
In the case of ref *not* being used, yeah.
Ok, so that means if the object(class instance) gets an update (member
changes) that when I use the handle to the object, it should reflect the
updated value. Right?
Yep. That's why the following works prints 'testing', not 'test':
using System;
using System.Text;
class Test {
static void Main() {
StringBuilder sb = new StringBuilder("test");
Append(sb);
Console.WriteLine(sb);
}
static void Append(StringBuilder sb) {
sb.Append("ing");
}
}
And can if you did use the 'ref' keyword, wouldn't it then pass an
object reference to an object by reference instead of by value?
An object reference would be passed to a method by reference instead of by
value.
That would mean that would be more like a pointer to a pointer. What
that means is that I could change the object that is being pointed to by
the first pointer and the second reference would automatically be
pointing to that 2nd object.
Indeed. This is how I tend to visualise it (with the arrows meaning 'refers
to'):
reference --> object reference --> object (on heap)
The object reference in the middle can be changed via the reference on the
left to refer to another object on the heap (or null).
For example, in the following 'False' is printed:
using System;
using System.Text;
class Test {
static void Main() {
StringBuilder sb = new StringBuilder("test");
StringBuilder original = sb;
PseudoAppend(ref sb);
Console.WriteLine(Object.ReferenceEquals(sb, original));
}
static void PseudoAppend(ref StringBuilder sb) {
// modify the original object reference to refer to
// a new object
sb = new StringBuilder("testing");
}
}
Had the StringBuilder been passed by value instead (i.e. had 'ref' not been
used) 'True' would've been printed as the object reference Main.sb would
not have been modifiable from within PseudoAppend.