Difference between String and StringBuilder class in Asp.Net C#

Introduction: In this article i am going to explain the difference between String and StringBuilder class in ASP.NET with C# and When to use String and when to use StringBuilder and why? It is one of the important interview questioned asked in interview.

String and StringBuilder class are used to handle the strings but there are subtle differences between these two.

Let's understand the difference between String and StringBuilder using suitable example:

1. Performance of StringBuilder is high as compared to String

Consider the following concatenation example:

String str= "";
str += "Welcome ";
str += "to ";
str += "webcodeexpert.com";

Output string will be Welcome to webcodeexpert.com

Let's do the same concatenation operation using StringBuilder class

StringBuilder sb = new StringBuilder("");
sb.Append("Welcome");
sb.Append("to ");
sb.Append("webcodeexpert.com");
String str = sb.ToString();

Output string will be Welcome to webcodeexpert.com

Have you noticed the result using String and StringBuilder is same; then what is the difference between these two? The difference is in their performance. Whenever we concatenate new string to the old string then String class always create new object of string type in memory where as when you use string builder class for that purpose, it updates string at one place in memory and does not create new space in memory. 

So obviously StringBuilder consumes less memory thus perform faster operation. So it is always recommended to use StringBuilder class for operations that manipulate strings. StringBuilder will give better performance as compared to String when concatenating the strings like shown above in example.

2) From the above point it is clear that String consumes more memory as compared to StringBuilder

3)  String Concatenation can be done using '+' operator or using the String.Concat method whereas in case of StringBuilder we use Append() method to concatenate.

4) String is Immutable (Non Modifiable) whereas StringBuilder is a Mutable Class

String is immutable i.e. the value stored in the String object cannot be changed. We cannot change it after creating it. Every time we use one of the methods in the System.String class that manipulate the string, we are actually creating a new string object in memory, which requires a new allocation of space for that new object. Internally a new String object is created each time whenever we do the changes. 

For example, when we use the "String" object to concatenate two strings, the first string is concatenated to the other string by creating a new copy in the memory as a string object, and then the old string is deleted so we can say when we create a string we can never change it, rather it will create a new string to store the new value. 

Hence it can cause performance issue in situations where we need to perform repeated modifications to the string because overhead associated with creating a new String object can be costly.

StringBuilder is mutable. By mutable we mean that we can perform the operations that manipulate the string without creating the new StringBuilder object each time.

For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop. So StringBuilder is more efficient if large amounts of string manipulations have to be performed.

4) String class belongs to the namespace System while StringBuilder class belongs to the namespace System.Text.

5) When string concatenation takes place, additional memory will be allocated whereas in case of StringBuffer additional memory will be allocated only when the string buffer capacity exceeds.

When to use String or StringBuilder Class?
For simple String manipulations we can use String class. But for large string manipulations it is recommended to use StringBuilder class.

Now over to you:
" I hope you have got the clear understanding of String and StringBuilder class now and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

28 comments

Click here for comments
JABBAR
admin
December 18, 2013 ×

Nice Works and concepts...

Reply
avatar
Anonymous
admin
December 18, 2013 ×

Nice explanation..bt i don't understand properly how to manage memory when i use StringBuilder class .. please explain in details.. otherwise this article is nice ..

Reply
avatar
December 19, 2013 ×

your welcome..i am glad you like this article..:)

Reply
avatar
December 19, 2013 ×

Thanks JABBAR for appreciating my work..keep reading for more useful updates like this..:)

Reply
avatar
Unknown
admin
December 22, 2013 ×

Thank you so much for nice article.

Reply
avatar
December 22, 2013 ×

thanks Kadir ansari for appreciating my work..i hope you enjoyed this article..:)

Reply
avatar
December 24, 2013 ×

Thanks man... i love your write.. very understand.. btw, why we talk 'memory' when the tekhnology over chipest.. :)

Reply
avatar
December 24, 2013 ×

your welcome...stay connected and keep reading for more useful updates like this..:)

Reply
avatar
January 01, 2014 ×

Your welcome Danish Khan.:)

Reply
avatar
Unknown
admin
January 02, 2014 ×

Yes Interesting & Informative

Reply
avatar
January 06, 2014 ×

Thanks mani arjunan..i am glad you liked my article..stay connected and keep reading..

Reply
avatar
Unknown
admin
February 15, 2014 ×

Best way to make understand!!

Thanks sir!!

Reply
avatar
February 15, 2014 ×

Thanks Dharmendra for appreciating my work. it is always nice to hear that my articles helped anyone .so stay connected and keep reading..:)

Reply
avatar
Anonymous
admin
March 07, 2014 ×

simple and best...

Reply
avatar
March 08, 2014 ×

Thanks...it is always nice to hear that my article helped anyone..Stay connected and keep reading for more useful updates like this one..:)

Reply
avatar
Unknown
admin
March 22, 2014 ×

thanks lalit..,

Reply
avatar
March 23, 2014 ×

Hi Devarajan..thanks for appreciating my work...it is always nice to hear that someone liked my work..:)

Reply
avatar
Anonymous
admin
April 15, 2014 ×

Thanks lalit,Nice article :)

Reply
avatar
Java
admin
April 15, 2014 ×

thank youuu very much..

Reply
avatar
April 15, 2014 ×

Hi java..i am glad you found this article helpful..

Reply
avatar
April 15, 2014 ×

Thanks for your valuable feedback..:)

Reply
avatar
Anonymous
admin
April 15, 2014 ×

Awesome. .. While doing program I usually use string. Going forward I prefer to use string builder. Your article makes me to understand the difference between both. Thank you so much..

Reply
avatar
April 26, 2014 ×

thanks for your feedback..keep reading for more useful information like this..:)

Reply
avatar
shahbaz
admin
October 09, 2014 ×

Have to say that you are brilliant in way you explained the concept

Reply
avatar
October 09, 2014 ×

Thanks Shahbaz for appreciating my work..Stay connected and keep reading for more useful articles..:)

Reply
avatar
June 30, 2015 ×

Nice Article :)

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..