Home  >  Article  >  Backend Development  >  A brief explanation of the usage of Builder and Buffer classes in C#

A brief explanation of the usage of Builder and Buffer classes in C#

巴扎黑
巴扎黑Original
2017-05-15 09:27:143069browse

This article will give you a brief introduction to the usage of the three classes String StringBuilder and StringBuffer in C#. Friends who need it can refer to it

String, StringBuilder and StringBuffer, these three are worth studying in depth. Yes, many people may say that if it doesn't work, just use StringBuilder and it won't matter. I can't say that your idea is incorrect, but I can give better suggestions. Below is a brief introduction to these three categories.

String class

In our daily use, it is easy not to notice that the code written by ourselves is easily boxed. Operation (convert value type to reference type). For example, it is very common, a string concatenation

string str=9+"test"; You can know that a boxing operation has occurred here by looking at the IL code. So it is recommended to use it (remember to use the ToString method when converting the value type to a string). So when you usually write code, you should pay attention to the boxing and unboxing operations (the generic collections introduced later are just to solve the boxing and unboxing operations). The process of boxing operation: put the value type into the managed heap to allocate memory. In addition to the memory allocated by the value type itself, the total memory must also add the memory occupied by the type object pointer and synchronization index block, and then the value The value of the type is reallocated into the heap memory, and finally the address of the object of the reference type is returned.

We know that many types (value types) come with a ToString method. Why don’t you use this to avoid boxing operations? It’s a simple truth. No one told you that String is special. Value type (although it is a reference type), the designer of the C# language, in order to achieve this, he came up with this method

The String object cannot be changed once assigned (called the constancy of the string ), after the assignment is completed, if the String is spliced, assigned, etc., a new memory space will be reallocated in the memory.

StringBuilder

Based on the above problem, the string class will reallocate memory space when reassigned, so in order to solve this problem, Microsoft launched 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 that

internal const int DefaultCapacity = 0x10; StringBuilder class, the default size is 16, which means that if we do not Specify the length of StringBuilder. If it exceeds 16 lengths, the memory will be reallocated. For specific implementation, you can look at the Append source code of the StringBuilder class.

It can be seen from the code that when we usually use StringBuilder, we must specify the appropriate length. The fixed statement block in the source code (in layman's terms, you can disable garbage collection and recycle the variable address).

Summary: When writing code, you should pay attention to boxing and unboxing operations, and pay attention to the use of stringBuilder.

【Related recommendations】

1. Special recommendation:"php "Programmer Toolbox" V0.1 version download

2. ASP free video tutorial

3. Li Yanhui ASP basic video tutorial

The above is the detailed content of A brief explanation of the usage of Builder and Buffer classes in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn