Click here to Skip to main content
13,095,218 members (80,730 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 19 Apr 2004

String Vs StringBuilder (C#)

, 1 May 2004
Rate this:
Please Sign up or sign in to vote.
An article on performance of String Vs StringBuilder and better places where each can be used


This is my second article regarding performance. Most of the people use string everywhere in their code. Actually when doing string concatenation, do you know what exactly you are doing? It has a big drawback mainly in concatenation which can be overcome by StringBuilder. It will give a vast improvement in performance when you use concatenation of st<code>ring over String.

What is the Exact Difference?

First we will look at what happens when you concatenate two strings. For a rough idea, think like this. In a loop, you are adding few numbers to get a string to give all the numbers.

string returnNumber = "";
for(int i = 0; i<1000; i++)
    returnNumber = returnNumber + i.ToString();

Here we are defining a string called returnNumber and after that, in the loop we are concatenating the old one with the new to get a string. Do you know when we do like that we are assigning it again and again? I mean it's really like assigning 999 new strings!

Actually the concatenation will create a new string returnNumber, with both old returnNumber and i.ToString(). If we think roughly, how will the performance of the code be? Can you imagine it? No one thinks about this when coding.

If we can have something which is to be defined only once and add all the strings into it, what can you say about the performance. That's what StringBuilder does.

StringBuilder returnNumber = new StringBuilder(10000);
for(int i = 0; i<1000; i++)

We are creating a StringBuilder of length 10000 in memory where we can add all the strings. This surely won't create a new string each and every time. Actually we are creating a StringBinder, and whenever something is added it will get copied into that memory area. At the end, we can get the string by StringBuilder.ToString(). Here also, it won't create a new string. It will return a string instance that will point to the string inside the StringBuilder. See, how efficient this is?

To explain this in a practical manner, I'm not going to analyze IL code or Optimized JIT compiled code. You can see the differences by running the samples.

Why String? Can't Use StringBinder Everywhere?

No. You can't. When initializing a StringBuilder, you are going down in performance. Also many actions that you do with string can't be done with StringBinder. Actually it is used mostly for situations as explained above. Last week, I showed a person who used StringBuilder to just add two strings together! It's really nonsense. We must really think about the overhead of initialization. In my personal experience, a StringBuilder can be used where more than four or more string concatenations take place. Also if you try to do some other manipulation (like removing a part from the string, replacing a part in the string, etc.), then it's better not to use StringBuilder at those places. This is because we are anyway creating new strings. Another important issue. We must be careful to guess the size of StringBuilder. If the size which we are going to get is more than what is assigned, it must increase the size. This will reduce its performance.


  • 19th April, 2004: Article submitted


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chester Ragel
Web Developer
Singapore Singapore
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionString builder Pin
Member 897143523-Apr-15 21:13
memberMember 897143523-Apr-15 21:13 
SuggestionSuggesting another source for learning difference between string and stringbuilder. Pin
Amit Technology Programmer26-Mar-15 6:38
memberAmit Technology Programmer26-Mar-15 6:38 
GeneralExpected more Pin
Member 1119035013-Mar-15 4:17
memberMember 1119035013-Mar-15 4:17 
GeneralRe: Expected more Pin
tomicrow17-Mar-15 22:03
membertomicrow17-Mar-15 22:03 
GeneralMy vote of 3 Pin
Afzaal Ahmad Zeeshan13-Nov-14 4:10
professionalAfzaal Ahmad Zeeshan13-Nov-14 4:10 
GeneralMy vote of 3 Pin
sibeesh17-Jul-14 7:28
membersibeesh17-Jul-14 7:28 
QuestionConfusion Regarding StringBuilder Class Pin
Saddam Khan Ranjhani1-Apr-14 23:08
memberSaddam Khan Ranjhani1-Apr-14 23:08 
AnswerRe: Confusion Regarding StringBuilder Class Pin
calzakk27-Oct-14 4:01
membercalzakk27-Oct-14 4:01 
SuggestionMemory Pin
Member 1028141517-Sep-13 21:32
memberMember 1028141517-Sep-13 21:32 
GeneralMy vote of 5 Pin
Member 950731410-Sep-13 21:44
memberMember 950731410-Sep-13 21:44 
GeneralMy vote of 1 Pin
Mukesh_B18-Feb-13 22:26
memberMukesh_B18-Feb-13 22:26 
GeneralVote of 4 Pin
Ismail482-Jan-13 2:09
memberIsmail482-Jan-13 2:09 
SuggestionThought I'd share, great way to automate Stringbuilder object creation Pin
Suck League20-Oct-12 8:10
memberSuck League20-Oct-12 8:10 
GeneralMy vote of 3 Pin
Ryan Leeder24-May-12 19:00
memberRyan Leeder24-May-12 19:00 
GeneralMy vote of 4 Pin
ehab.abuzied23-May-12 3:23
memberehab.abuzied23-May-12 3:23 
GeneralMy vote of 2 Pin
Senthilkumar Elangovan6-Apr-12 10:45
memberSenthilkumar Elangovan6-Apr-12 10:45 
GeneralMy vote of 1 Pin
Member 846447828-Dec-11 7:30
memberMember 846447828-Dec-11 7:30 
GeneralMy vote of 4 Pin
awaisdar11-Dec-11 7:00
memberawaisdar11-Dec-11 7:00 
GeneralMutable vs Immutable Pin
Vozzie230-Nov-10 3:37
memberVozzie230-Nov-10 3:37 
GeneralGood Article Pin
ShalinD27-Mar-10 19:30
memberShalinD27-Mar-10 19:30 
Generalthnx Pin
goher16-Feb-10 2:07
membergoher16-Feb-10 2:07 
GeneralStringBuilder Vs String Pin
Suresh271298-Oct-07 21:11
memberSuresh271298-Oct-07 21:11 
GeneralImprove Performance with Unnessary String Creation Pin
CodeEnjoy2-Aug-07 2:59
memberCodeEnjoy2-Aug-07 2:59 
GeneralSome good Blog entries on .net String performance Pin
andy brummer29-Apr-04 6:54
memberandy brummer29-Apr-04 6:54 
GeneralStringBuilder is OFTEN not faster Pin
Kenneth Beckett29-Apr-04 6:01
memberKenneth Beckett29-Apr-04 6:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 2 May 2004
Article Copyright 2004 by Chester Ragel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid