Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's not a case of which is more useful...</p> <p>A <code>String</code> is a <code>String</code> - one or more characters next to eachother. If you want to <strong><em>change</em></strong> a string in someway, it will simply create more strings because they are <strong><em><a href="http://www.yoda.arachsys.com/csharp/strings.html" rel="nofollow noreferrer">immutable</a></em></strong>.</p> <p>A <code>StringBuilder</code> is a class which creates strings. It provides a means of constructing them without creating lots of reduntant strings in memory. The end result will always be a <code>String</code>.</p> <p><strong>Don't do this</strong></p> <pre><code>string s = "my string"; s += " is now a little longer"; </code></pre> <p><strong>or</strong></p> <pre><code>s = s + " is now longer again"; </code></pre> <p>That would create <strong>5</strong> strings in memory (in reality, see below).</p> <p>Do this:</p> <pre><code>StringBuilder sb = new StringBuilder(); sb.Append("my string"); sb.Append(" is now a little longer"); sb.Append(" is now longer again"); string s = sb.ToString(); </code></pre> <p>That would create <strong>1</strong> string in memory (again, see below).</p> <p>You can do this:</p> <pre><code>string s = "It is now " + DateTime.Now + "."; </code></pre> <p>This only creates <strong>1</strong> string in memory.</p> <p>As a side-note, creating a <code>StringBuilder</code> does take a certain amount of memory anyway. As a rough rule of thumb:</p> <ul> <li>Always use a <code>StringBuilder</code> if you're concatenating strings in a loop.</li> <li>Use a <code>StringBuilder</code> if you're concatenating a string more than 4 times.</li> </ul>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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