A brief description of the usage of builder and buffer classes in C #

Source: Internet
Author: User
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; StringBuilderclass, 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

Related Article

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.