As we are learning to program in C# or while we are attending any interview as C# developer, everyone should have encountered this question of , Which is better to user StringBuilder or string class while are manipulating the string content? And with 100% surety we claim that StringBuilder class is better from the performance point of view. In this article I will explain why is it better then string class.
The reason that we know the performance supremacy of the StringBuilder class is that it is mutable and String class on the other hand is immutable. If we are doing any changes to the string like concatenation, there is always an creation of the new instance of the string and new reference is returned.
But that is not the case with StringBuilder class. Any alteration on this class is worked on the same instance of the class. The StringBuilder internally uses an array of characters to work on. As soon as we instantiate StringBuilder class, an array of the characters is created with size of 16 which is known as the capacity of the StringBuilder class. Arrays are better to work from the performance point of view as these are continuous blocks of memory created, which makes foster to retrieve or insert data into an array.
If the size of the string on which StringBuilder class is working is more than 16 characters or whatever is the current capacity , in that case the capacity of the array is doubled and more memory blocks are added to the character array.
The below code snippet confirms the capacity concept
StringBuilder sb = new StringBuilder(); Console.WriteLine("Initial Capacity: " + sb.Capacity); sb.Append("I am more than 16 chrancters"); Console.WriteLine("Capacity: " + sb.Capacity); sb.Append("I am more than 16 chrancters"); Console.WriteLine("Capacity: " + sb.Capacity); sb.Append("I am more than 16 chrancters"); Console.WriteLine("Capacity: " + sb.Capacity); sb.Append("I am more than 16 chrancters"); Console.WriteLine("Capacity: " + sb.Capacity); sb.Append("I am more than 16 chrancters"); Console.WriteLine("Capacity: " + sb.Capacity); sb.Append("I am more than 16 chrancters"); Console.WriteLine("Capacity: " + sb.Capacity); Console.Read();
The output of the above code is as shown below
In the above output the initial capacity for the StringBuilder class is 16 but as soon as it finds that the original length of the string to be appended is more it doubles its size and keep doing the same if the size of array is not adequate to hold the whole string.
I hope this article has helped to understand why StringBuilder class is better in performance.
Dmitry Bytchenko says
Typical exceptions of the rule are IMHO as much as valuable the rule itself
First, a degenerated case worth mentioned: when concatetaing two strings only a direct implementation
String result = a + b;
is a better choice than combersome (and slow)
String result = new StringBuilder(a.Length + b.Length).Append(a).Append(b).ToString();
The second case is the compile time concatenation:
String result = “some” + ” ” + “compile” + ” ” + “time” + ” ” + “sample”;
When compiling the compiler will concatenate the string for you.
Vikram Chaudhary says
Awesome..thanks a lot for addition