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:
00795130
Vues:
22
Hi Cathi,

Thanks, as I metnioned to Claudio, this is what I expected but wsn't sure. And we pretty much verified this by creating a couple of functions that assign strings in a single statement and then assign multiple strings to the same variable through multiple assignment statements, then looked at the low level code using ILDASM. In the single assignment statement the overloaded function Concat is called once with multiple string arguments while in the second case Concat is called multiple times. Interesting.

Bill

>Hi Bill,
>
>If you put your string into one statement then you don't need to use the StringBuilder. The StringBuilder should be used it you keep reassigning the string variable to new values. Each time you reassign, you are creating a new instance with just enough memory allocated to store the combined text. The original string is on the heap now ready for garbage collection. You can run into performance issues with strings over time if you reassign them.
>
>>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?
>>
>>Thanks
>>Bill
William A. Caton III
Software Engineer
MAXIMUS
Atlanta, Ga.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform