The difference between c#:string, StringBuffer and StringBuilder

Source: Internet
Author: User

A good brain is better than a bad pen, always remember, write it down

Still handling address: http://blog.csdn.net/qq_28187979/article/details/76607253

--------------------------------------------------------------------------------------------------------------- --------

Recently on these silly points are not clear, in the online collection of some information, now summed up.

The string is the base type, and the first statement, in effect, is to create an object of type string named S1, and the system allocates enough memory for S1 to hold the data in S1. The second statement, instead of adding the string "Ceshi" to the address pointed to by S1, creates a new string instance, allocates enough memory to it, puts "test" into it, adds "Ceshi" to it, and then updates the memory address stored in the S1 variable, making S1 point to the new string object , the old string object is destroyed. That is, each time you add or delete a string type, a new string object is created and the old string object is destroyed. If you perform string operations frequently, you can affect system performance. This is also why a string is an immutable object. Therefore, if you need to modify the string frequently, it is recommended to use StringBuffer or StringBuilder.

string S1 = "Test";

S1 + = "Ceshi";

StringBuilder and string initialization, when string is initialized, the system allocates enough memory to hold its defined string literals, but StringBuilder has many constructors to initialize the initial size of the current instance and the maximum number of characters that can be stored. When used, it is best to set the capacity to the maximum length of the string, to ensure that the StringBuilder does not need to reallocate memory, and if the number of characters exceeds the set maximum capacity, the. NET runtime automatically allocates memory and doubles. That is, StringBuilder can explicitly set the size of the allocated memory, and string can only allocate enough memory by the system based on the size of the initialization string.
In the above case, StringBuffer and StringBuilder are the same, the latter faster than the former, but the former is thread-safe, suitable for multi-threaded use, the latter is thread non-security, more suitable for single-threaded use.

The difference between c#:string, StringBuffer and StringBuilder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.