A
Alvin Bruney
The more knowledgable I get about this .net world, the more questions I
have.
..NET uses pass by reference for all objects....uhhh I mean pass by value.
(Couldn't resist this jab)
Consider a string object whose value is passed as a parameter to a function.
The reference is pointing back to memory allocated on the heap where the
contents of the string lives. Inside the function, I adjust the value, say
string += "value", which means on the surface that the string object in
memory gets changed. That's the 10,000 foot view. It's not correct, but
widely repeated. Strings are immutable, so what exactly is going on
underneath the hood with this scenario?
Is it right that the run-time must now allocate new memory and copy the old
contents into this new location in order to honor the immutability of
strings?
If that's true, then the run-time must know explicitly that the memory
location behaves differently for a string so it must mark that block of
memory as special. Other objects would not behave this way hence the need
for special treatment.
I thought memory wasn't treated that way. Isn't it true for other languages
and environments that memory is just a block which gets no special
treatment? Why has .NET chosen to go with this meandering? What are the
benefits of this special treatment because it looks to me, from where I sit
that the overhead associated with this approach isn't worth the price of
admission.
I've been wrong before so don't humor me. Give it to me straight, I'm old
enough to take it.
have.
..NET uses pass by reference for all objects....uhhh I mean pass by value.
(Couldn't resist this jab)
Consider a string object whose value is passed as a parameter to a function.
The reference is pointing back to memory allocated on the heap where the
contents of the string lives. Inside the function, I adjust the value, say
string += "value", which means on the surface that the string object in
memory gets changed. That's the 10,000 foot view. It's not correct, but
widely repeated. Strings are immutable, so what exactly is going on
underneath the hood with this scenario?
Is it right that the run-time must now allocate new memory and copy the old
contents into this new location in order to honor the immutability of
strings?
If that's true, then the run-time must know explicitly that the memory
location behaves differently for a string so it must mark that block of
memory as special. Other objects would not behave this way hence the need
for special treatment.
I thought memory wasn't treated that way. Isn't it true for other languages
and environments that memory is just a block which gets no special
treatment? Why has .NET chosen to go with this meandering? What are the
benefits of this special treatment because it looks to me, from where I sit
that the overhead associated with this approach isn't worth the price of
admission.
I've been wrong before so don't humor me. Give it to me straight, I'm old
enough to take it.