First, of all, you cannot "add to a string", ever. String objects are
immutable. In contrast to arrays, you cannot even write a different character in the position which is already available. All operations on strings, such as concatenation, in fact simply create a brand new string object and copy "old" data to it. If you assign the result of such operation to the same variable, a brand new string object is created first, data is copied, and eventually abandoned (
unreachable) "old" string object will be garbage-collected.
If you track all what happens, you will understand that repeated concatenation, such as expressions with several '+' suffer from poor performance, which can be a considerable factor when, for example, you try to do it in loop. Right approach to it is using the mutable type,
System.Text.StringBuilder
.
String.Format
also efficient and should always be used instead of an expression with multiple '+'. Please see:
http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.string.aspx[
^],
http://msdn.microsoft.com/en-us/library/b1csw23d.aspx[
^].
Now, I'm not sure you really understand the nature of the "set a new line". In fact, string with multiple lines is still a single string. The lines are just separated with one or more characters, "line separator":
http://en.wikipedia.org/wiki/Newline[
^].
As you can see, the line separator depends on the platform. To use it correctly, you should always use
System.Environment.NewLine
:
http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx[
^].
Finally, the simple text boxes (not RTF) don't have the operations appending a line. You always should rewrite the whole
Text
. If you really want to append, use
ListBox
.
—SA