Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As it is clearly mentioned in <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.textboxbase.appendtext(v=vs.110).aspx" rel="nofollow noreferrer">Remarks section of MSDN Documentation</a></p> <blockquote> <p>The AppendText method enables the user to append text to the contents of a text control without using text concatenation, which, can yield better performance when many concatenations are required.</p> </blockquote> <p>Your question,</p> <blockquote> <p>what is the difference between these two methods?</p> </blockquote> <p>We all know how <code>TextBox.Text += something;</code> will work i.e. creating and appending a new string each time but how <code>AppendText</code> works I could not find any code snippet whether internally it uses <code>StringBuilder</code> or something else.</p> <blockquote> <p>Is one more efficient than the other?</p> </blockquote> <p>I think answer to above question will depend on the situation, <strong>(Based on Test case observation)</strong></p> <blockquote> <p>if <strong><code>Multiline</code></strong> property is set to <strong><code>false</code></strong> then <strong>Concatenation (+=)</strong> yields better results but on other hand <strong><code>Multiline</code></strong> property is set to <strong><code>True</code></strong> then <strong><code>AppendText</code></strong> yields far better performance.</p> </blockquote> <p><strong>EDIT</strong> After reading the <a href="https://stackoverflow.com/questions/20632372/textbox-text-string-vs-textbox-appendtextstring/20632719#comment30886034_20632719">comment from Rawling</a> I made a custom win-form solution in which I had a simple <code>textbox</code> in which I appended a simple string <code>hello</code> <strong>10000</strong> times using a simple <code>for-loop</code> </p> <pre><code> private void btnAppendText_Click(object sender, EventArgs e) { txtText.Text = string.Empty; DateTime startTime = DateTime.Now; for (int i = 0; i &lt; 10000; i++) { txtText.AppendText(s); } DateTime endTime = DateTime.Now; txtTime.Text = (endTime.Ticks - startTime.Ticks).ToString(); } private void btnConcante_Click(object sender, EventArgs e) { txtText.Text = string.Empty; DateTime startTime = DateTime.Now; for (int i = 0; i &lt; 5000; i++) { txtText.Text += s; } DateTime endTime = DateTime.Now; txtTime.Text = (endTime.Ticks - startTime.Ticks).ToString(); } </code></pre> <p><strong>Output</strong> were very surprising,<br> <strong>TEST 1: Multiline property is true</strong> I had to reduce the iteration to half i.e. 5000 for text concatenation as it was talking very long time</p> <blockquote> <p><code>btnAppendText_Click</code> output on <code>txtTime</code> was <strong><code>37222129</code></strong> almost 3-4 seconds for 10000 iteration<br> <code>btnConcante_Click</code> output on <code>txtTime</code> was <strong><code>14449906487</code></strong> more then 25 minutes for only 5000 iteration</p> </blockquote> <p>From the above result it is really clear that, <code>AppendText</code> is much faster and efficient (when <code>Multiline</code> is <code>True</code>) then <code>Concatenation</code></p> <p><strong>TEST 2: Multiline property is false</strong> </p> <blockquote> <p><code>btnConcante_Click</code> output on <code>txtTime</code> was <strong><code>39862280</code></strong> almost 3-4 seconds for 10000 iteration<br> <code>btnAppendText_Click</code> output on <code>txtTime</code> was <strong><code>1043279672</code></strong> almost 2-3 minutes for 10000 iteration</p> </blockquote> <p>From the above result it is really clear that, Concatenation is faster and efficient (when <code>Multiline</code> is <code>false</code>) then <code>AppendText</code></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. 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