Note that there are some explanatory texts on larger screens.

plurals
  1. POString immutability and interning in .net
    primarykey
    data
    text
    <p>I was going to post an answer on <a href="https://stackoverflow.com/questions/10290209/store-all-the-results-of-a-loop-in-a-single-string">this other SO question</a> but someone went ahead of me with the StringBuilder class, anyways, digressing...</p> <p>I understand that strings in .NET are immutable, so creating a string and modifying it will actually create two different strings in memory, contrary to the <code>StringBuilder Class</code> which maintains an internal buffer and creates a final string only on the <code>ToString()</code> call. I also know that .NET interns each string, so only a copy will be created per string ever used in the application.</p> <p>When you append a string to another one they are both being used in the application, so they both get interned (don't they?), the main reason for not to append strings with the concatenation operator is because you will end up with a whole bunch of strings in memory when done, each larger than the previous.</p> <p>I understand that concatenating <code>01234</code> with <code>56789</code> will result in the following two strings in memory:</p> <blockquote> <p>01234 <br> 0123456789</p> </blockquote> <p>whilst using the <code>StringBuilder</code> will only yield, due to interning:</p> <blockquote> <p>01234 <br> 56789</p> </blockquote> <p>obviously the second approach is better for performance but, how good is it actually? I mean, if you're creating a bigger string from a small list of values, why should I consider the <code>StringBuilder</code> (that per se takes memory space)? Is it <strong>always</strong> good to use it? Or is there any good rule of thumbs as to when [not] to use it?</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.
 

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