Inevitable misunderstanding of lines or an error in Documents?

I just saw this in MS Visual Studio documents, and the part in bold does not make sense to me. Is this wrong or do I not get it right? If you run this, b will appear to hold "hello" (as you would expect), not "h".

Strings are immutable - the contents of a string object cannot be changed after the object is created, although the syntax makes it look like you can do it. 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";

+5
source share
13 answers

You have done the addition and assignment in one step. Strings are immutable, but also a reference type.

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

We can look at pseudo-memory as follows:

string b = "h";         // b    := 0x00001000 ["h"]
string tmp1 = "ello";   // tmp1 := 0x00002000 ["ello"]
string tmp2 = b + tmp1; // tmp2 := 0x00003000 ["hello"]
string b = tmp2;        // b    := 0x00003000 ["hello"]

I'm not quite sure where you get this text, because after reading the documentation for string , I find (not that I think that "h" actually gets garbage collection):

Strings are immutable - the contents of a string object cannot be changed after the object is created, although the syntax makes it look like you can do it. For example, when you write this code, the compiler actually creates a new string object to hold the new sequence of characters, and the new object is assigned b. Then the string "h" has the right to garbage collection.

@Jon Skeet , "h" , - , , , # Standard , § 2.4.4.5. :

. , (§7.9.7), , .

+7

, , . , . - , :

b ""

OP, :

(1) , , (b) ( , ) , "" . (2) , b "h", "hello" . .

b - "h" "".

"", , " ". , , b "h".

, , , :

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

, a, -, "h"; b , , b, .

( #, .)

+5

, . ( . .)

, "", , . :

string b = "h";
b = string.Concat(b, "ello");

- , .

+4

. b "". , .

+4

:
- , . , b , .
, , , .
, , - () .
, , , , , .

. , , , , .

. :

string b = "h";
string m1 = b;
b += "ello";
// now b == "hello", m1 == "h"

b "", m1 "h". , "h" "", . b + = "ello" , "" b, b "b".

, m1 "", "b", b m1 .

+4

, . , , :

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

, :

   string b="hello";
   if(b[0] == 'h')  // we can read via indexer
      b[0] = 'H';   // but this will fail.
+2

. - "h", - "ello", - "". b "hello". .

+1

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

b - . , "+ =", b "h". "hello", "h" "ello". "h" GC.

0

, , , "hello", b, , "" b "h", , . - . this .

0

, #, Java, Java :

b = "h";

b = ( StringBuilder (b)). ( "ello" ). ToString();

, "+" "Append" , .

0

:

string b = "h";
string c = b + "ello";    // b still == "h", c = "hello"
string d = string.concat(b, "ello"); // d == hello, b still "h"

b "h"? "b" , reference. , b, . , , :

string b = "ello";
string f = b.Insert("h",0);

b "hello" ( h 0), inmutable, b "ello".

, .

b = "ello";
b = "Some other string";
// b not references "Some other string" , but the object "ello" remains unchanged.

, ( : S)

0

, ( )

0

, " ". , ID #123 "h" ID #547 "ello". b = "h"; b ID #123. b += "ello"; ID #123 ID #547 + , , , String.Concat. , , (, ID #915) System.String, "hello" . ID #915 b.

0

All Articles