As the title suggests, is there any reason I shouldn't do the following to append something on to the end of a string:
string someString = "test";
someString += "Test";
If done once, it's no big deal. The content of the two strings is taken, a new string is created and assigned to someString. If you do the above operation many times (for example, in a loop), then there is a problem. For each execution, you're allocating a new object in the heap, a new string instance. That's why it's better to use a buffer-based type, such as StringBuilder, if you have to append content to a string multiple times.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With