Inside – String & StringBuilder, which and when ?

Inside – String & StringBuilder, which and when ?

Well folks,  even if you think that you know the answer and it is very common, I still think you should read this post to make your concept more clear, because strangely I found there are lot developer who still have some  confusion with the concept even though they read tons of articles in the web about the same topic. This post will give you a bit internal on using String and StringBuilder.

To start with,

Strings are immutable. That means, when you manipulate a string in C#, it creates another string instance in a separate location on heap and points to that location without remembering the previous instance.

Ok, I think you all know this sentence as you might have already came across this and the sentence above is 100% true.
Let’s have a look bit inside

string x = "Daily .NET Tips";
string y = "Daily .NET Tips";

Does it say, x and y will have two different memory locations?

Naah… not at all.

C# does a great job with Strings when dealing with intern. String maintains an intern table which stores all the strings that have been either statically typed as above or used in the program itself. So both x and y variables are initialized with one single address where the string stays.

String Allocation

Now let us consider another example :

string x = "Daily .NET Tips";
string y = "Useful Tips and Tricks for .NET Developer";

string z = x + y;

Now here in the above call, x and y are interned into the string intern table but still z creates a memory because strings are immutable and z will point to another separate location which stores the entire content of Z.

String Concat

Here lies the problem. In case you do like:

string z = x + y + " Follow this site at Twitter and Facebook";

The problem became more acute. As strings are immutable, the above line creates a heap allocation for x + y and stores it and then again it creates another memory for storing x + y + “Follow this site at Twitter and Facebook”.

String Concat Operation

Well, this is wastage of memory you can see. Any string operation, like Replace, Remove, Substring etc, will have this problem.

Lets’s have a look at StringBuilder

StringBuilder is a cure for this. StringBuilder stores a character array without using the inner implementation of C# strings and creates a string instance only when the ToString is called for, thus ensuring that StringBuilder becomes mutable.

That means if you write :

StringBuilder z = new StringBuilder();
z.Append(x);
z.Append(y);
z.Append(" Follow this site at Twitter and Facebook");

The object z is maintained in heap as a single array instance of characters which keeps on storing the string into it and any replace or removal of some characters inside the StringBuilder will mean the Array gets manipulated not the actual string.

StringBuilder in Heap and Stack

I hope this post helped you understanding the exact differences.

Do let me know if you still have confusion in this.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434

4 Comments to “Inside – String & StringBuilder, which and when ?”

  1. Sree Hari

    hi Abhishek, in first case it was fine two strings are same, so they are referring to same memory in heap. In second example x and y are two different strings but still they refer to same memory in.. bit confused..

  2. Abhishek Sur Author

    Hi Sree Hari,

    Yes,
    You got a bug. The arrows in the image is wrong. I will fix that and update you shortly.

    For time being, the Second and 3rd case y will point to diff memory.

    Sorry for the mistake.

Comments are closed.