Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, the operator <code>+=</code> can't be overloaded. If you have the expression <code>A += B</code>, it's compiled as if you wrote:*</p> <pre><code>A = A + B </code></pre> <p>Okay, that's why <code>string</code> doesn't overload <code>operator +=</code> (because it can't be overloaded). So, why doesn't it overload <code>operator +</code> either? It's just one more difference between the CLR and C#. The C# compiler knows that types like <code>string</code> and <code>int</code> are special, and it generates special code for their operators (calling <code>string.Concat()</code> for <code>string</code>, or the <code>add</code> instruction for <code>int</code>).</p> <p>Why are these operators treated in a special way? Because you want them treated in a special way. I think this is most clear for <code>int</code>:</p> <ol> <li>You don't want each <code>int</code> addition to be compiled as a method call, that would add a lot of overhead. Because of that, special instruction for <code>int</code> addition is used.</li> <li>Integer addition doesn't always behave the same with regards to overflows. There is a compiler switch to throw exceptions for overflows and you can also use the <code>checked</code> and <code>unchecked</code> operators. How should the compiler deal with that if it had only <code>operator +</code>? (What it actually does is to use the instruction <code>add</code> for unchecked overflows and <code>add.ovf</code> for checked overflows.)</li> </ol> <p>And you want to treat <code>string</code> addition in a special way too, for performance reasons. For example, if you have <code>string</code>s <code>a</code>, <code>b</code> and <code>c</code> and write <code>a + b + c</code> and then you compiled that as two calls to <code>operator +</code>, you would need to allocate a temporary <code>string</code> for the result of <code>a + b</code>, which is inefficient. Instead, the compiler generates that code as <code>string.Concat(a, b, c)</code>, which can directly allocate only one string of the required length.</p> <hr> <p>* This is not exactly right, for details, see Eric Lippert's article <a href="http://blogs.msdn.com/b/ericlippert/archive/2011/03/29/compound-assignment-part-one.aspx"><em>Compound Assignment, Part One</em></a> and §7.17.2 of C# specification. Also note the missing semicolons, <code>A += B</code> really is an expression, for example, you can write <code>X += Y += Z;</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.
    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