title: The += operator with nullable types in C#
body: <p>In C#, if I write </p>
  
  <pre><code>int? x = null;
  x += x ?? 1
  </code></pre>
  
  <p>I would expect this to be equivalent to:</p>
  
  <pre><code>int? x = null;
  x = x + x ?? 1
  </code></pre>
  
  <p>And thus in the first example, <code>x</code> would contain <code>1</code> as in the second example. But it doesn't, it contains null. The += operator doesn't seem to work on nullable types when they haven't been assigned. Why should this be the case?</p>
  
  <p><strong>Edit</strong>: As pointed out, it's because <code>null + 1 = null</code> and operator precedence. In my defence, I think this line in the <a href="http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx">MSDN</a> is ambiguous!: </p>
  
  <blockquote>
    <p>The predefined unary and binary operators and any user-defined
    operators that exist for value types may also be used by nullable
    types. These operators produce a null value if <em>[either of]</em> the
    operands are null; otherwise, the operator uses the contained value to
    calculate the result.</p>
  </blockquote>