4

Being new to C#, I was reading some guide. About strings, here I read (highlighting is mine):

Strings are immutable--the contents of a string object cannot be changed after the object is created, although the syntax makes it appear as if you can do this. For example, when you write this code, the compiler actually creates a new string object to hold the new sequence of characters, and the variable b continues to hold "h".

string b = "h";
b += "ello";

But trying the following code, it prints "hello".

string b = "h";
b += "ello";
System.Diagnostics.Debug.WriteLine(b);

So, am I misinterpreting what I read, or is the documentation wrong? Any other option? :)

Antonio
  • 19,451
  • 13
  • 99
  • 197
  • 1
    The documentation seems confusing to me as well. As far as I know b loses reference to "h" and gets a new reference to the new string "hello". Maybe they just expressed themselves a bit unclear – Sossenbinder Sep 04 '15 at 13:17

3 Answers3

7

Clearly the documentation is wrong and as you have already found a latter version has been corrected, although it too has some issues (See below). But I think a better example would be

string b = "h";
string d = b;
d += "ello";

Now b is still "h" because the += did not update the reference, but create a new string that d references.

Also it should be noted that there are 3 strings in this code. First the string literal "h", then the string literal "ello" and finally the string "hello" that is created from the concatenation of the previous two. Because all string literals are interned and interned strings are not garbage collected the only string of the 3 that will eventually be eligible for garbage collection is the "hello" that is currently referenced by d. Although it is possible to turn string interning off in which case all three would eventually be eligible for garbage collection.

juharr
  • 31,741
  • 4
  • 58
  • 93
2

Ok, I see the problem is that I was looking at Visual Studio 2005 documentation, which was simply wrong. The right updated documentation is here.

Note that the documentation had already been amended for Visual Studio 2008:

Strings are immutable--the contents of a string object cannot be changed after the object is created, although the syntax makes it appear as if you can do this. For example, when you write this code, the compiler actually creates a new string object to hold the new sequence of characters, and that new object is assigned to b. The string "h" is then eligible for garbage collection.

But, as correctly pointed out by juharr in the comments below, it was still wrong in the sense that "h" is interned and never garbage collected.

Antonio
  • 19,451
  • 13
  • 99
  • 197
  • Yeah that looks more like it is supposed to be – Sossenbinder Sep 04 '15 at 13:18
  • I don't think anything changed -- it's just badly worded in the old documentation. – Jeroen Vannevel Sep 04 '15 at 13:21
  • 2
    @adv12 What I have highlighted in my question is certainly wrong. The rest I think I understand. I agree juharr answer gives a much better example. – Antonio Sep 04 '15 at 13:23
  • @adv12 The old documentation was not fine. It was in fact wrong. – Matthew Watson Sep 04 '15 at 13:23
  • Oh, I see. Sorry for adding to the confusion. – adv12 Sep 04 '15 at 13:24
  • Though I have to wonder about the new documentation saying _The string "h" is then eligible for garbage collection._ Because string literals are interned and so it might actually not be garbage collected. – juharr Sep 04 '15 at 13:37
  • @juharr I think the documentation means `The old string reference by the b variable and containing "h" is now eligible for garbage collection`. It's an acceptable simplification of the concept, much better of the previous version at least :) – Antonio Sep 04 '15 at 13:39
  • @Antonio It does a better job of explaining the concept of immutability and references, but since `string` is used as the example it's technically wrong. http://stackoverflow.com/questions/8692303/intern-string-literals-misunderstanding. And it doesn't even bother to mention what happens with the "ello" `string` that was also created. – juharr Sep 04 '15 at 13:40
  • @juharr Wow!! Yes, that's another pretty interesting thing to know. So, also in 2008 the documentation was wrong, just at least a little bit less dangerously wrong :) – Antonio Sep 04 '15 at 13:43
-1

Looks like the documentation is wrong: d += "ello"; is a shortcut for d = d + "ello";

The string object itself is immutable but you assign a new string to an existing variable (d).

Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189