There are no of article and post says that : StringBuilder is more efficient because it does contain a mutable string buffer. .NET Strings are immutable which is the reason why a new string object is created every time we alter it (insert, append, remove, etc.).
In following post I am going to explain the same thing in more detail to give the beginners more clear view about this fact.
I wrote following code as you can see I have defined one string variable and one StringBuilder variable. Here I am appending string to both type of the variable
After the execution of above code
Now to get in more details what happened when append string I use red-gate .Net Reflector.
Below image shows the IL of the code that I executed above as you can see
When I clicked Contact function in reflector it redirect me to the below code.
As you see Concat function take two argument and return string, following steps performed when we execute append with string type
So this proves that whenever i do operation like concatenation of strings, it creates new strings 'coz of immutable behaviour of strings.
Note:
Above scenario occurs for all the function related to string operation.
When I clicked on Append function in reflector it redirect me to the below code.
Append function takes one argument of type string and returns StringBuilder object , following steps performed when we execute append with StringBuilder type
So it returns same object without creating new one.
Summary
I hope this article help you to understand inner details about the fact why to use StringBuilder over string to get better performance when you do heavy/major string manipulations in your code.
In following post I am going to explain the same thing in more detail to give the beginners more clear view about this fact.
I wrote following code as you can see I have defined one string variable and one StringBuilder variable. Here I am appending string to both type of the variable
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace StringTest { class Program { static void Main(string[] args) { string s = "Pranay"; s += " rana"; s += " rana1"; s += " rana122"; StringBuilder sb = new StringBuilder(); sb.Append("pranay"); sb.Append(" rana"); } } }
After the execution of above code
s= "pranay rana rana1 rana12" sb = "pranay rana"
Now to get in more details what happened when append string I use red-gate .Net Reflector.
Below image shows the IL of the code that I executed above as you can see
- When I append string its execute function Concate(str0,str1) to append string.
- When I append string using StringBuilder its call’s Append(str) function.
When I clicked Contact function in reflector it redirect me to the below code.
As you see Concat function take two argument and return string, following steps performed when we execute append with string type
- Check for the string is null or not
- Create string dest and allocate memory for the string
- Fill dest string with str0 and str1
- Returns dest string, which is new string variable.
So this proves that whenever i do operation like concatenation of strings, it creates new strings 'coz of immutable behaviour of strings.
Note:
Above scenario occurs for all the function related to string operation.
When I clicked on Append function in reflector it redirect me to the below code.
Append function takes one argument of type string and returns StringBuilder object , following steps performed when we execute append with StringBuilder type
- Get string value from StringBuilder object
- Check the its require to allocate memory for the new string we going to append
- Allocate memory if require and append string
- If not require to allocate memory than append string directly in existing allocated memory
- Return StringBuilder object which called function by using this keyword
So it returns same object without creating new one.
Summary
I hope this article help you to understand inner details about the fact why to use StringBuilder over string to get better performance when you do heavy/major string manipulations in your code.
Lots of beneficial reading here, many thanks! I was searching on yahoo when I uncovered your article, I’m going to add your feed to Google Reader, I look forward to a lot more from you.
ReplyDelete