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
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.
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.
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”.
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 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.
I hope this post helped you understanding the exact differences.
Do let me know if you still have confusion in this.