Note that there are some explanatory texts on larger screens.

plurals
  1. POString Concatenation Vs String Builder Append
    primarykey
    data
    text
    <p>So...I have this scenario where I have a Foreach loop that loops through a List of Checkboxes to check which are selected. For every selected checkbox, I have to do a pretty long string concatenation, involving 30 different strings of an average length of 20 characters, and then send it out as a HTTP request. 2 of the strings are dependant on the index/value of the checkbox selected.</p> <p>The length of the List of Checkboxes is also variable depending upon the user's data. I would say the average length of the List would be 20, but it can go up to 50-60. So the worst case scenario would be performing the whole string concatenation 60 or so times.</p> <p>For now I'm doing it with simple string concatenation via the '+' operator, but I'm wondering if it would be faster to do it with Stringbuilder. Of course, that means I'd have to either create a Stringbuilder object within the loop, or create it before the loop and call Stringbuilder.Remove at the end of it after sending out the HTTP request.</p> <p>I appreciate any insights anybody can share regarding this issue.</p> <p><strong>EDIT</strong><br> Thanks for all the replies everybody, so from what I've gathered, the best way for me to go about doing this would be something like:</p> <pre><code> StringBuilder sb = new StringBuilder(); foreach (CheckBox item in FriendCheckboxList) { if (item.Checked) { sb.Append(string1); sb.Append(string2); sb.Append(string3); . . . sb.Append(stringLast); SendRequest(sb.ToString()); sb.Length = 0; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload