


A brief explanation of the usage of Builder and Buffer classes in C#
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.
