Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you are concerned only with memory I would suggest:</p> <pre><code>foreach (string outputString in outputStrings) { string output = "prefix " + outputString + " postfix"; ExistingOutputMethodThatOnlyTakesAString(output) } </code></pre> <p>The variable named output is the same size in your original implementation, but no other objects are needed. StringBuilder uses strings and other objects internally and you will be created many objects that need to be GC'd.</p> <p>Both the line from option 1:</p> <pre><code>string output = formattedOutput.ToString(); </code></pre> <p>And the line from option 2:</p> <pre><code>ExistingOutputMethodThatOnlyTakesAString( formattedOutputInsideALoop.ToString()); </code></pre> <p>will create an <em>immutable</em> object with the value of the prefix + outputString + postfix. This string is the same size no matter how you create it. What you are really asking is which is more memory efficient:</p> <pre><code> StringBuilder formattedOutput = new StringBuilder(); // create new string builder </code></pre> <p>or </p> <pre><code> formattedOutput.Remove(0, output.Length); // reuse existing string builder </code></pre> <p>Skipping the StringBuilder entirely will be more memory efficient than either of the above.</p> <p>If you really need to know which of the two is more efficient in your application (this will probably vary based on the size of your list, prefix, and outputStrings) I would recommend red-gate ANTS Profiler <a href="http://www.red-gate.com/products/ants_profiler/index.htm" rel="nofollow noreferrer" title="http://www.red-gate.com/products/ants_profiler/index.htm">http://www.red-gate.com/products/ants_profiler/index.htm</a></p> <p>Jason</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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