Cor Ligthert said:
Ok than, maybe I wrote it wrong and did Jon not understand that.
A string is immutable, what you are showing is not a reference *to an
existing object*.
I hope that this what I thought what was obvious makes it more clear.
No, it's not obvious that's what you meant. "... what you are showing
is not a reference" and "... what you are showing is not a reference to
an existing object" are completely different statements, just like
"there are 29 days in February" and "there are 29 days in February in a
leap year" are completely different statements.
The funny thing is that when we *do* extrapolate what you actually
write into what we think you meant to write, you accuse us of putting
words into your mouth. Now you're trying to say we *should* try to
guess what you actually meant to write instead of believing that you
mean what you actually *do* write.
However, let's look at your revised statement:
"A string is immutable, what you are showing is not a reference to an
existing object."
Do you mean that the string must be newly created in the GrabLongString
method? If so, that's entirely untrue. GrabLongString() could be
implemented by:
return new string ('y', 1000); // Reference to newly created object
or
return someInstanceVariable; // Reference to "old" object
Furthermore, the "age" of an object has little to do with whether or
not it's eligible for garbage collection. Here are four different
implementations of GrabLongString:
1) New object, but keep object reference in a member variable: it won't
be eligible for garbage collection:
string value;
string GrabLongString()
{
value = new string('x', 10000);
return value;
}
2) New object, no other references to it: it will be eligible for
garbage collection after the call to Console.WriteLine in the calling
method:
string GrabLongString()
{
return new string ('x', 100000);
}
3) Existing object, we still have a reference to it after the method
returns: it won't be eligible for garbage collection:
string value = new string ('x', 10000);
string GrabLongString()
{
return value;
}
4) Existing object, but we change the value of the variable which was
holding the previous reference. Assuming nothing else references the
same string, it will be eligible for garbage colleciton after the call
to Console.WriteLine in the calling method:
string value = new string ('x', 10000);
string GrabLongString()
{
string ret = value;
value = null;
return ret;
}
I hope the above makes it clear that the value of x in the calling
method certainly *can* be a reference to an existing object, and that
it's irrelevant to garbage collection.
Furthermore, there's nothing in the above which is unique to strings,
or even to immutable types. *All* reference types obey the same rules.
Jon does not know that I think.
Know what, exactly? I still think that you're the one with some
incorrect ideas about strings here - however, you haven't articulated
those ideas clearly enough to prove them definitely wrong.
Perhaps you could try again to say in what way you think strings differ
from other reference types when it comes to garbage collection? If
you'd talked about string *literals* we'd be onto something, but you
haven't mentioned them so far, and most strings aren't created from
literals.
I asked you to specify clearly how you thought I was wrong a few posts
ago, but you chose to ignore technical arguments and concentrate on
personal ones...