C# - When to use StringBuilder

Hi,

I'm a VB guy transitioning to C#.  In VB, I had no qualms liberally using "&" to concatenate strings.  I understand that there may be some performance disadvantages, but my applications aren't demanding or high performance apps.

But C# developers tend to use more "proper" technique, so I recognize that building a string using "+" may be frowned upon, and that StringBuilder may be the preferred approach.

However, is there some threshold where + is acceptable?  i.e. what if I'm only concatenating two short strings?  What if 3 or 5 short strings?  Is there some point at which StringBuilder offers a benefit?  And is performance the only benefit of StringBuilder?  Or does it have other benefits, like buffer overflow / exploit protection?

I don't need hard performance numbers--anecdotes are fine.

Some typical examples of things I need to do:

errorMsg = "An error occurred while trying to query " + objectName + " records."

sqlFilter = "CUSTNMBR LIKE '%" + filter + "%' AND DATE BETWEEN '" + date1 + "' AND '" + date2 + "'"

Is StringBuilder really necessary / recommended for these types of simple concatenations?

Thanks,

Steve
LVL 18
Steve EndowMicrosoft MVP - Dynamics GPAsked:
Who is Participating?
 
silemoneConnect With a Mentor Commented:
errorMsg = "An error occurred while trying to query " + objectName + " records."

sqlFilter = "CUSTNMBR LIKE '%" + filter + "%' AND DATE BETWEEN '" + date1 + "' AND '" + date2 + "'"

your above code should be ok...
again, its when you're dynamically creating large string query... and you continuously update the string...

string x = "Hello"
x = x + "hello again"
.
.
.
x = x + "added about a hundred lines and this cause a hundred strings objects";
0
 
silemoneCommented:
use stringbuilder when your string will continue to grow...
this is because you don't want to use string because it will keep creating instances of it...
0
 
silemoneCommented:
stringbuilder is more efficient in those times as i stated before...
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
silemoneCommented:
hope this links helps for a better explanation

http://www.codeproject.com/KB/cs/StringBuilder_vs_String.aspx
0
 
silemoneCommented:
and definitely for query building, stringbuilder would be ideal especially if the query is really large...each time you do this

string x = "Hello"
x = x + "hello again"

a new x is created and it starts weighing you program down...

so if there was a stack of objects it would look like

x    Hello
x.1 Hello again
x.2 Hello again, again...
basically each string is a new string object weighing the program down...
0
 
silemoneCommented:
stringbuilder on the other hand on a stack of objects would be:

stringbuilder bu = new string builder...


it would only appear in your object stack once...no matter how much you add to it...
0
 
silemoneCommented:
here's a test you can do and time it yourself...

http://www.codeproject.com/KB/string/string.aspx
0
 
Steve EndowMicrosoft MVP - Dynamics GPAuthor Commented:
Thanks for the article.  Unfortunately that article addresses String vs. StringBuilder, not string concatenation using "+" with a string object (lowercase).

I found this article, which seems to imply that for simple concats like the ones I am doing, the performance difference is zero / negligible in any practical terms.  

http://www.c-sharpcorner.com/UploadFile/mahesh/StringBuilderComp11232005235258PM/StringBuilderComp.aspx
 
 Again, let me point out that I am only concatenating a few (less than 5) very short strings, not 10,000 strings in a loop.

The consensus seems to be that C# developers prefer StringBuilder because it is the "proper" technique, but for simple situations, it may not offer any distinct advantages.
0
 
silemoneCommented:
as i stated in comment  ID: 26114390
and my personal explanation (excuse wrong link)...for large string building, use string builder...
otherwise it would be negligible...5 lines won't hurt anything, but it would still be good practice to start...
0
 
silemoneCommented:
Also I've coded in VB and used stringbuilder there also...not counting vb6...so in general its good to get use to the practice...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.