Home | ASP.Net & C# |
MVC
|
Others
|
What is the difference between String and StringBuilder in C# with Example
Introduction
As we all know, in C# we have string and StringBuilder for string manipulation. Have you ever think of why we need these 2. Everything that we can do with StringBuilder can be done with string also. Yes. But there is one important scenario where we need to use StringBuilder instead of string.
Most of us are usually using string in our code when we need to do any manipulation on string data. Usage of string and StringBuilder plays an important role when it comes to performance. There is a serious performance issue when we are using string for concatenation which can be overcome by using StringBuilder. Lets check both of them and see how it is.
String
We can simply say string is immutable. So what is immutable means. Immutable means, once we created a string object, we cannot modify it. All the operations on that string ( Replace, append, modification etc) will discard the old object and create a new instance in the memory.
This is actually equal to creating 100 strings.
Just keep in mind this about string for now. And we can now see what StringBuilder is and after that we can see how StringBuilder is important for performance.
StringBuilder
StringBuilder is mutable. This means, once we create a StringBuilder object and any operations performed on that object will not create new object in memory. So when it comes to performance, if we have many append operation on a string, we need to go for StringBuilder instead of string.
Another important thing about StringBuilder is , when we are creating a StringBuilder object without specifying the capacity, it will have a capacity of 16 characters by default. And if we are trying to append more than one character after 16, it will discard the old StringBuilder object and create a new one. The newly created object will have double capacity than the old one. That is 32 characters. And appending string more than 32 characters will create an object with 64 characters. See the code below.
Here I have declared a StringBuilder with specifying its capacity as 10000 characters. So it will create an object in memory with capacity of 10000 characters. And if we append a character more than 10000, it will create a new object with 20000 characters. So it is always a performance booster if we can able to specify approximate size of the characters that we are going to save in StringBuilder object.
I can personally say, it is simply a non sense to create a StringBuilder object without specifying the number of characters. Also if we are using StringBuilder for concatenating 2 or 3 strings, it is also useless. Using string is better here. Because, considering the performance overhead of creating StringBuilder over string, it is advisable to create StringBuilder if you have more than 5 string concatenation.( Personal opinion )
Performance comparison using string and StringBuilder
Now we can check whether the above said regarding performance is true if we are using StringBuilder. :) .
I am using a string object to concatenate 25000 strings. I am displaying the time before concatenation starts and after concatenation done.
And the output of above code is..
It took almost 4 seconds to complete the concatenation.
Now we can use StringBuilder for the same. See the code below
The output of the above code is
See this.. the StringBuilder object did not take even 1 second for concatenation. Here we can see the performance impact when we are using string for large concatenations.
I hope you all understood the differences and the places where we need to use StringBuilder.
We can say the differences between string and StringBuilder as
- String is immutable, StringBuilder is mutable.
- StringBuilder shows more performance than string
- string belongs to System namespace. But StringBuilder belongs to System.Text namespace.
No comments:
Post a Comment