Saturday 7 May 2016

String And StringBuilder

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.

Coding
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace StringAndStringBuilder  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            string name = "Ramakrishna";  
            name = "Ramakrishna Basagalla";  
        }  
    }  
}  

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.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace StringAndStringBuilder  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            string name = "Ramakrishna";  
  
            for (int i=1;i<=10000;i++) {  
                name = "Ramakrishna " + i;  
            }  
        }  
    }  
}  
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. 
String Builder

String Builder is mutable, it will update/replace the existing value with new value.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace StringAndStringBuilder  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            StringBuilder name = new StringBuilder();  
  
            for (int i=1;i<=10000;i++) {  
                name.Append("Ramakrishna " + i);  
            }  
        }  
    }  
}  
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 CLR Profiler here are the results.

Conclusion
String
StringBuilder
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



No comments:

Post a Comment