jones065 (3) [Avatar] Offline
#1
This goes with all due respect since I am finding this book extremely useful, but I keep re-reading MYTH #3 in Chapter 2 and it's gobbledygook. Jon keeps using the word reference as a noun, an adjective, an adverb and a verb. It would be much easier on the reader to use more exact terminology than "reference" for everything that is meant. I'm not convinced he is making any worthwhile argument when he uses the code snippet:


void AppendHello(StringBuilder builder)
{
builder.Append("hello");
}


and then says:

"If I were to change the value of the builder variable within the method - for example with the statement [i]builder = null; --- that change wouldn't be seen by the caller, contrary to the myth"[/i]

Being the devil's advocate, if you create two local instances of StringBuilder and assign the second instance to the first instance and then set the second instance to null, you will get the same result. The first instance will still be alive and well whereas the second instance will be null:

StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = sb1;
sb2 = null; // sb2 is now null because it doesn't point to sb1 anymore, but sb1 is still referencing a valid object.

So, this change isn't being seen locally either and kind of contradicts Jon's argument since it seems to say this is something that happens only if you use a reference type passed as an argument to a method.