Difference between String and String Builder in .Net

String

String is always immutable, the memory will allocate separately whenever we assign new value to that, below is the best example to explain this.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace StringAndStringBuilder  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             string name = "Ramakrishna";  
  14.             name = "Ramakrishna Basagalla";  
  15.         }  
  16.     }  
  17. }  
In this above example the string variable ‘name’ is allocating memory twice, it will maintain the two set of values in memory, it is hard to believe that but internally it uses this concept.

To explain it better see the below example.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace StringAndStringBuilder  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             string name = "Ramakrishna";  
  14.   
  15.             for (int i=1;i<=10000;i++) {  
  16.                 name = "Ramakrishna " + i;  
  17.             }  
  18.         }  
  19.     }  
  20. }  
In the above example I have created ‘name’ string variable and assigned this variable with multiple times using for loop, as we discussed strings are immutable so it is going to allocate multiple copies for the names (here it is going to create 100000 copies).

To test this we have a tool called ‘CLR Profiler’, we can download this tool from the Microsoft site, this tool is going to capture how much memory is allocated and it shows options like the memory management graph (How memory is handled by the Garbage Collector).

So let’s run this tool by targeting the application.

I have extracted in my local folder, it will give us two version of the codes (32 and 64 bit codes).

codes
Execute the CLRProfiler and select the Allocation and Calls checkboxes and choose our application exe to run. (Click on Start Application)

Application
Application

Here the application allocated 400,236,631 bytes to store he string names.

As I mentioned string are immutable because to support thread safety, if the string variable is used by different threads we need to create multiple concepts for the string variables.

We can overcome this problem using String Builder.

String Builder

String Builder is mutable, it will update/replace the existing value with new value.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace StringAndStringBuilder  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             StringBuilder name = new StringBuilder();  
  14.   
  15.             for (int i=1;i<=10000;i++) {  
  16.                 name.Append("Ramakrishna " + i);  
  17.             }  
  18.         }  
  19.     }  
  20. }  
In the above example I have used same example to update variable but instead of string I am using string builder to append.

If we run this application using CLRProfiler here are the results.

results

Here the application allocated bytes is 136,597, it is far better than the string (400,236,631 bytes).

Conclusion

String String Builder
String are immutable String Builder is mutable
Memory is allocated separately for each assign, it will create separate copy. It replace the existing one with new one.
String are slow compare with String Builder String Builder is faster than String
This will be suggested for less string assign This will be more useful if you assign values multiple times
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now