C #-String and StringBuilder

Source: Internet
Author: User

A String object is called immutable (read-only), and because its value cannot was modified after it had been created. Methods that appear to modify a string object actually return a new string object that contains the Modi Fication.

Because strings is immutable, string manipulation routines that perform repeated additions or deletions to what appears t O is a single string can exact a significant performance penalty.

< Span style= "line-height:18px" >you can use the  stringbuilder  class instead of the  string  class for operations That does multiple changes to the value of a string. Unlike instances of the  string  class,  stringbuilder  objects is mutable; When you concatenate, append, or delete substrings from a string, the operations is performed on a single string.

A&NBSP; stringbuilder  object maintains a buffer to accommodate expansions to the string. New data is appended to the buffer if hostel is available; Otherwise, a new, larger buffer is allocated, data from the original buffer was copied to the new buffer, and the new data is then appended to the new buffer.

although The stringbuilder  class generally offers better performance than The string  class, should not automatically replace string  with stringbuilder  whenever You want to manipulate strings. Performance depends on the size of the string, the amount of the memory to is allocated for the new string, the system on whic H your app is executing, and the type of operation. You should is prepared to test your app to determine Whether stringbuilder   Actually offers a significant performance improvement.

Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

C #-String 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.