This article gives you a brief introduction to the use of string StringBuilder StringBuffer three classes in C #, the need for a friend to refer to it
String and StringBuilder and StringBuffer, these three are worth a dig, perhaps a lot of people will say, really not, all with StringBuilder, what is not, I can not say that your thoughts are not correct, But I can give better advice. Here's a brief look at these three classes.
String class
It's easy to notice in our usual use that the code you write is very easy to get boxed (converting a value type to a reference type). As is common, a string concatenation
string str=9+"test";
by looking at the IL code, you know that a boxed operation has occurred here. It is recommended that you use the ToString method when converting a value type to a string. So when writing code, you should note that boxing and unpacking operations (after the introduction of the generic collection is not to solve the boxing and unpacking operations). The process of boxing: placing a value type in the managed heap allocates memory, in addition to the memory allocated by the value type itself, the total amount of memory plus the memory occupied by the type object pointer and the synchronized index block, then reassigning the value type's values to the heap memory, and finally returning the address of the object of the reference type.
We know that many types (value types) come with a ToString method, which does not occur in the case of a boxing operation, very simple, not someone and you say string is a special value type (although he is a reference type), the C # language designer, in order to do this, He came up with this method
The string object, once the assignment is immutable (called the constant character of the string), after the assignment, and then concatenation of the string, assignment, and so on, will re-allocate a new memory space.
StringBuilder
Based on the above problem, the string class will re-assign the memory space again, so in order to solve this problem, Microsoft introduced a StringBuilder class. You can see how the StringBuilder class does not reallocate memory.
By reading the implementation of the Stringbuiler class, we can find
internal const int DefaultCapacity = 0x10; StringBuilder
class, the default size is 16, meaning that if we do not specify StringBuilder length, more than 16 lengths, we will allocate memory again. Concrete implementation can see StringBuilder class of Append source code.
From the code can be seen, we usually use StringBuilder, we must specify the appropriate length. The fixed statement block inside the source code (in layman's terms, you can prohibit garbage collection to recycle the variable address).
Summary: Usually when writing code should pay attention to boxing unpacking operation, pay attention to the use of StringBuilder.
"Recommended"
1. Special recommendation : "PHP Programmer Toolkit" V0.1 version download
2. asp free Video Tutorial
3. Eon the ASP Basic video tutorial