Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
String vs. StringBuilder
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00794481
Message ID:
00795131
Vues:
15
Hi David,

Thanks for the input. I'll take a look at the article. As I mentioned to Cathi, we pretty much verified your conclusions. The scenario I described is actually not debugging code in this case but storage of input data in formatted bulk form on the backend so had specific application to me.

Bill

>I wrote an article on the String and StringBuilder classes for the UT last year:http://www.levelextreme.com/Magazine/April2002/Page41.asp. It explains some of the issues with String immutability and Interning and has some guidelines for which class to use.
>
>For your code, the "\r\n" + "Field2: " concatenation will be optimized by the compiler at compile time so there is no runtime penalty there. But at least 40 string objects will have to created on the heap and garbage collected. Still, I don't have any hard numbers on where the efficiency of StringBuilder starts to be more important than the convenience of string.
>
>The bottom line: unless this is a critical section of code (and it looks more like debugging code than anything else) your code is fine. Unless you plan on having hundreds or thousands of concatenations. In that case it would be easier to put it in a loop and use StringBuilder just to save typing!
>
>
>>I know that when many operations are performed on a string or strings such as in a large loop, a StringBuilder should be used isntead of a String. However, what I am curious about is if this applies to creating a single string object from a set of known concatenated strings in a single statement.
>>
>>E.g., if we have 20 fields on a form and want to create a single string containing all of the values, we could do something like:
>>string MyString = "Field1: " + this.MyField1.Text + "\r\n" +
>>                  "Field2: " + this.MyField2.Text + "\r\n" +
>>                  "Field3: " + this.MyField3.Text + "\r\n" +
>>                  ...
>>                  "Field20: " + this.MyField20.Text
>>Since all of the data is concatenated into the single string in one statement, does the rule to use a StringBuilder for multiple operations apply in this case?
William A. Caton III
Software Engineer
MAXIMUS
Atlanta, Ga.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform