Microsoft C # String Documentation: Am I misinterpreting what I read, or is the documentation wrong?

Being new to C #, I read several tutorials. About the lines, here I read (my selection):

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

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

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

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

So, am I misinterpreting what I am reading, or incorrect documentation? Any other option? :)

+4
3

, , , , (. ). ,

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

b - "h", += , string, d .

, 3 strings. string "h", string "ello" , , string "", . string , , string 3, , - "", d. string, .

+7

, , , Visual Studio 2005, . .

, Visual Studio 2008:

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

, , , "h" .

+2

The documentation seems to be wrong: it d += "ello";is a shortcut tod = d + "ello";

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

-1
source

Source: https://habr.com/ru/post/1605948/


All Articles