In my previous tutorials, I’d explained the difference between string and String, difference between executereader executenonquery and executescalar, difference between dataset datareader dataadapter and dataview and other similar tutorials on Difference, Asp.net here.
Now here in this tutorial, I’ll explain the main difference between String and StringBuilder with examples for understanding in asp.net c# as well as vb.net.
Main Differences between String and StringBuilder
|String is immutable, meaning that once you created string object then you can’t modify it||StringBuilder is mutable, meaning that once you created string builder object then you can modify or append without creating a new instance for every time|
|Any operation that appears to change the string, it creates a new instance of string type in memory||Any operation that appears to modify or append new string, it doesn’t create a new instance of string type in memory|
|Required Class is System.String||Required Class is System.Text.StringBuilder|
|Required Namespace is System||Required Namespace is System.Text|
|String is slower as compare to string builder object when we deals with large size of strings||StringBuilder is faster as compare to string object when we deals with large size of strings|
|String is efficient when we deals with static or smaller size of string means we don’t need to modify later on||StringBuilder is efficient when we deals with dynamic or larger size of string means we want to modify that string later on|
Example of String – [C#]
Here is the example of String in C#:
string strEfficientTest = "AspnetO – Way To Learn Asp.net";
//not efficient as it'll create new instance while appending sting
string strNotEfficientTest = "AspnetO";
strNotEfficientTest += " – Way To ";
strNotEfficientTest += "Learn Asp.net";
Example of String – [Vb.net]
Likewise, following is the example of String in vb.net:
Dim strEfficientTest As String = "AspnetO – Way To Learn Asp.net"
'not efficient as it'll create new instance while appending sting
Dim strNotEfficientTest As String = "AspnetO"
strNotEfficientTest &= " – Way To "
strNotEfficientTest &= "Learn Asp.net"
Here in the above example, The strEfficientTest is more efficient than strNotEfficientTest because memory being allocated for strNotEfficientTest when it initially assigned the value of “AspnetO” and when you append ” – Way To ” and “Learn Asp.net” to it, then the .NET framework will allocate a new memory location for it to be able to combine all in one as “AspnetO – Way To Learn Asp.net”.
Example of StringBuilder – [C#]
Here is the example of StringBuilder in C#:
StringBuilder sbEfficientTest = new StringBuilder("AspnetO");
sbEfficientTest.Append(" – Way To ");
//now you can get combined string by using this single statement
string strStringBuilder = sbEfficientTest.ToString();
Example of StringBuilder – [Vb.net]
Likewise, following is the example of StringBuilder in vb.net:
Dim sbEfficientTest As New StringBuilder("AspnetO")
sbEfficientTest.Append(" – Way To ")
'now you can get combined string by using this single statement
Dim strStringBuilder As String = sbEfficientTest.ToString()
However with StringBuilder, The .NET framework only allocates the memory once for the sbEfficientTest variable when it initialize first time with the word “AspnetO”, when you append the string ” – Way To ” and “Learn Asp.net” to it, it’ll still share the same memory location.
Ultimately, as per following example you’ll notice no difference at all in execution speed. However, take a look at the following example:
for (int i = 0; i < 10000; i++)
strTest += i.ToString() + " ";
You notice it’ll take longer than the following same example using StringBuilder:
for (int i = 0; i < 10000; i++)
As you can see the first example will loop through “10000” and creating 10000 * 2 instances in memory allocation where as in case of StringBuilder it’ll give much less stress on the .NET framework memory allocator without creating new instances.