The following article, What is StringBuilder in C# provides an outline for the StringBuilder in C#. Before talking about StringBuilder, lets first see the concept of immutability, if you are acquainted with the Strings then probably you would be knowing that strings have immutable nature i.e. each time a new object is created and new memory location is provided to it and hence whenever data change happens, the changed data is to be placed into a new memory location and keeping the original string safe. Now, let us pay little thought. What is the use case of this approach? Well, if you are setting credentials like database details, other credentials which won’t be changed in the transient time, then you can use String for that to keep them safe. Now there comes another kind of object, so-called StringBuilder, i.e. mutable in nature, hence saves memory for you by allowing you to modify data in the current memory location only. Such things come in handy when you are looking to have multiple append operations to come your way.
StringBuilder is a class that represents a mutable set of characters and it is an object of the System. Text namespace. StringBuilder is a dynamic object in nature and hence StringBuilder doesn’t create a new object in the memory, rather it dynamically expands the required memory to incorporate the modified or new string. String builders don’t appear convincing in a multithreading environment as they can result in data inconsistency when written over by multiple threads if the multithreading concept is not used properly.
1. Now the point comes where one shall understand the results mutability can lead to, you should choose to use mutability only if the same set of data is going to expand dynamically, Let’s take an example to understand this scenario.
2. If you are taking some data from an HTML form which is to be stored into a table based on some business logic, which will first validate data, in that case, you can create a StringBuilder object and append all the data fields, in association with the columns of database and finally can execute an insert query to place that all in database, the advantage of such approach is that all the time new memory location was not supposed to be allocated. And C# will be managing that one memory location with dynamic expansion for you.
3. You can also specify the number capacity of StringBuilder in C#.
StringBuilder s=new StringBuilder(10);
StringBuilder s=new StringBuilder("object value", 15);
4. The StringBuilder.length property indicates the number of characters the StringBuilder object currently contains. If you add characters to the StringBuilder object, its length increases until it equals the size of the StringBuilder.
There are certain methods provided in C#, which makes working with StringBuilder easy. StringBuilder class provides the following methods to make the developer’s task easy –
Example –
classStringBuilder a = new StringBuilder("val",10); a.append("us")
when you do console.writeline(a), it will give you result “value”
Example –
StringBuilder a = new StringBuilder("val",10); a.appendFormat("{0:C}",10)
Kindly execute this and analyze the variation from what has been done in the simple append() method.
Example
StringBuilder a = new StringBuilder("val",10); a.insert(4,"ue")
Execute and see the result
These all make a task for coding typical business problems easily and with fewer lines of code.
ArgumentOutOfRangeException.
OutOfMemoryException.
Well, we have discussed this in the above points only and one can infer here the use cases where this can come in handy.
Hence we saw the difference between string and StringBuilder, when to use what and what are the challenges or exceptions that can appear while working with the StringBuilder.
The above is the detailed content of What is StringBuilder in C#. For more information, please follow other related articles on the PHP Chinese website!