[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# - When to use StringBuilder

Posted on 2009-12-23
10
Medium Priority
?
571 Views
Last Modified: 2012-05-08
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
0
Comment
Question by:Steve Endow
  • 9
10 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 26114312
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
 
LVL 21

Expert Comment

by:silemone
ID: 26114330
stringbuilder is more efficient in those times as i stated before...
0
 
LVL 21

Expert Comment

by:silemone
ID: 26114336
hope this links helps for a better explanation

http://www.codeproject.com/KB/cs/StringBuilder_vs_String.aspx
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:silemone
ID: 26114354
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
 
LVL 21

Expert Comment

by:silemone
ID: 26114364
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
 
LVL 21

Accepted Solution

by:
silemone earned 2000 total points
ID: 26114390
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
 
LVL 21

Expert Comment

by:silemone
ID: 26114405
here's a test you can do and time it yourself...

http://www.codeproject.com/KB/string/string.aspx
0
 
LVL 18

Author Comment

by:Steve Endow
ID: 26114411
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
 
LVL 21

Expert Comment

by:silemone
ID: 26114500
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
 
LVL 21

Expert Comment

by:silemone
ID: 26114507
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question