Turn from: Delve into the inner workings of String and StringBuilder
The System.String type has always been a topic of constant discussion, a type that is used to store and manipulate strings.
System.String is also the only reference type in C # underlying types. However, it has many characteristics of value types.
Let's look at a simple code:
1 string " White " ; 2 string temp = text; 3 " Black " ; 4 Console.WriteLine (text); 5 Console.readkey ();
According to the theory of reference, where the TEMP variable should be the address of the stored text variable, then modify the value of the TEMP variable, and the value of text should be changed accordingly.
Then, the value of the text variable should be "Black", but in fact the value of the test text variable is "white".
That means the TEMP variable is definitely not the address of the stored text variable. However, this is contrary to the characteristics of the reference type, then what is the internal how to deal with it?
As far as I know, Microsoft should have introduced Copy-on-write (write-time copy) technology in String type, first to briefly explain what is Copy-on-write technology:
Simply put, when copying an object it is not true that the data of the original object is copied to another address in memory, but instead points to the same location as the original object in the memory mapping table of the new object.
and set the Copy-on-write bit of that memory to 1. When performing a read operation on this object, the memory data is not changed and can be executed directly.
At the time of writing, the original object is actually copied to the new address, modify the new object's memory mapping table to this new location, and then write here.
A programmer with some experience should know that Copy-on-write (copy-on-write) technology uses a "reference count" approach, and a variable is used to hold the number of references.
When the first class is constructed, the String's constructor allocates memory from the heap based on the parameters passed in, and the count is automatically incremented when other classes require the memory.
When there is a class destructor, this count will be reduced by one until the last class is refactored, at which point the reference count is 1 or 0, at which point the program will actually release the memory allocated from the heap.
To put it bluntly, "reference counting" is the principle of copy in the String Class!
In fact, string is still an immutable data type, and once an object of type string is initialized, the string object cannot be changed.
To illustrate this point, let's look at a small piece of code that is very simple:
1 string " Adamas " ; 2 " Is a gentleman. ";
When you execute this code, you first create a String type object named name and initialize it to "Adamas".
At this point, the. NET runtime allocates enough memory for the string to hold the text, and then sets the variable name to represent the string instance.
Syntactically, the second line of code is to add more text to this string. When I was a beginner of the C # language, I understood that as well.
This is not actually the case, but instead, create a new string instance, allocate enough memory for him, and then store all the merged text.
Copy the text from the first line of code: "Adamas" to the new string, plus the text in the second line of code: "is a gentleman.".
Then update the memory address stored in the name variable so that the variable points correctly to the new string object. The old string object is destroyed by the reference and waits for the system to be reclaimed.
This is not a problem in itself, but there is a huge performance problem if you need to do string operations frequently.
Therefore, in order to solve this problem, Microsoft introduced the System.Text.StringBuilder class. In the StringBuilder class, it is limited to replacing, adding, and deleting text in strings, but it is much more efficient than string.
New StringBuilder (a);
The StringBuilder class, when initialized, provides many constructors to initialize the initial size of the current instance and the maximum number of characters that can be stored, and a string to initialize the current instance.
In fact, when we create a StringBuilder object, the. NET runtime allocates a cache area in memory for the current object, reserving space for string operations.
When using the StringBuilder class, it is best to set the capacity to the maximum possible length of the string, ensuring that StringBuilder does not need to allocate memory repeatedly.
If the character's capacity exceeds the set maximum capacity, the. NET runtime automatically allocates memory and doubles.
For our. NET programmers, the difference between StringBuilder and String is that the StringBuilder can display the settings to allocate the size of the memory,
A string can only be allocated enough memory by the system based on the size of the string you initialize.
So, when it comes to frequent manipulation of strings, between string and StringBuilder, we should choose StringBuilder.
[Go] deep dive into the inner workings of String and StringBuilder